Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
standard data types
#1
Hello,

I just started to learn Python a bit. At the moment I am busy to get familiar with the standard data types. It looks pretty easy that there is only one int data type. Doesn matter the value you want to store. I made a very simple application, initialize a variable with a big value. For example with the value 120000000000000000001. Multiply this value by 3, store it in a the same or in an other variable. Then divide it by 3. The result is not the same as the start value. See code below.

startValue = 120000000000000000001
result_1 = (3 * startValue)
result_2 = int(result_1 / 3)
print(startValue)
print(result_1)
print(result_2)

Is in the above example a variabele or internal result saved as a floating point? Why is result_2 not equal to the startValue? Can this be solved? If so, how.
Reply
#2
Division (at least in Python 3.0+) returns a floating point number. Since the number you gave it is so large, it loses some precision when converted to floating point. If you want to force an integer result without the sidestep into floating point, you can do so with integer division (result_1 // 3).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
In fact int(float(startValue)) is not equal to startValue. StartValue needs 67 bits to be stored
>>> int(float(startValue))
120000000000000000000
>>> startValue.bit_length()
67
Floating numbers are stored on 64 bits from which only 53 are used for precision. A little bit of precision is lost in the conversion.
Reply
#4
Thanks Ichabod! Indeed this is the solution
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What data types can I use for default values? Mark17 1 484 Oct-09-2023, 02:07 PM
Last Post: buran
  In SQL Server, mix data types. shiv11 0 854 Sep-21-2022, 12:50 PM
Last Post: shiv11
  I need to add data types to cython conversion python to c Good_AI_User 1 973 Aug-19-2022, 07:52 AM
Last Post: Gribouillis
  How to insert different types of data into a function DrData82 0 1,225 Feb-10-2022, 10:41 PM
Last Post: DrData82
  Winsorized Mean and Standard Deviation Wheeliam 0 1,791 Jul-11-2020, 05:27 PM
Last Post: Wheeliam
  standard library modules chpyel 4 2,761 May-10-2020, 02:58 PM
Last Post: snippsat
  List of mixed data types to bytes medatib531 1 2,304 Mar-16-2020, 11:53 AM
Last Post: scidam
  Changing Data Types BallisticSwami 2 2,366 Jun-27-2019, 01:17 PM
Last Post: BallisticSwami
  Type error when reading in different data types on an __init__ method Dylanmull 3 2,748 May-09-2019, 02:05 PM
Last Post: buran
  Python validate excel values data types Useruser00 0 4,811 Apr-08-2019, 01:29 PM
Last Post: Useruser00

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020