Im stuck on how to write a function which get as an input ether
large string
large integer
and transform into a float.
The problem is when I transform into a FLOAT, it is missing characters.
#value= int(45264444.4444444444466666254188888888888526)
value= "45264444.4444444444466666254188888888888526"
float(value)
output:45264444.44444445
A float can not store arbitrary numbers, its precision is limited.
A float will store the few most significant digits of a number (in binary form), and discard the rest.
That is why you see the value truncated.
For a more in-depth look at the problem, see for example https://0.30000000000000004.com/, or some general guide on floating-point arithmetic.
Related
I am just learning python and making a calculator and the tutorial says float instead of int. what is a floating number
Why not just use int
Floats, contrary to int or integer data types, can store numerical data with much more precision. Integers lose information regarding to the mantissa or decimal portion of the number. So in applications where precision is required, we use float data type instead of int.
welcome to Stackoverflow!
This is a question on which you can dive deep, and I would absolutely google this. For example, the Wikipedia page on this will already give you a bunch of information.
Now, in order not to overwhelm you with a too much information, we can answer your question without too many details:
A float is a type with which you can represent decimal numbers (numbers with a comma in there, for example 0.5). An int is a type with which you can only represent integers (1, 2, 50, ...).
Don't hesitate to google around for more info!
A float is a number with numbers after decimal points, for example 12.56 . It can also be called a real.
An integer is a whole number eg 12.
If you use an integer in a calculator, then you wouldn't be able to calculate with anything other than whole numbers.
for example using int is fine if you do 12 + 12. However, if you enter 12.56 + 12.56, then you will get an error, as the int function assumes that the user enters a whole number
I would like to write a script in python that takes a float value for example -37.32 and output its IEEE value (11000010000101010100011110101110). for the first bit if the number is negative than it is 1 else it's 0. for the exponent its the matter of dividing the number by 2 and getting the remainder if I am correct. as for the mantissa, I have no idea how to calculate it. returning a string would be the most reasonable way since it would be better for constructing each element of the IEEE.
can someone help me with the approach I will be taking? or show a script tackling this problem and explaining it to me?
The struct module can be used to convert a float to a sequence of bytes. Then it's just a matter of converting each byte to a binary string and joining them together.
>>> import struct
>>> ''.join('{:08b}'.format(b) for b in struct.pack('f', -37.32))
'10101110010001110001010111000010'
I was programming a calculator and I came across float. I was told to use floats then my calculator would be able to calculate calculations including numbers like 16.4 and 4.5 and not just whole numbers.
Now, I sort of got thinking and wondered if someone could just verify I'm on the right tracks.
I understand an int is a solid number, a double is basically 2 numbers with a dot in the middle, a decimal? And now comes the tricky one, the float data type.
I just need somebody to verify I'm on the right track. I think a float is a data type that can either be a whole number (this is what I'm not sure about) and a double/decimal.
I feel like float is the safe data type kinda thing, where you're not sure and want to accept whole and decimal numbers, am I right? Is it for accepting both whole and decimals?
For anyone that struggles to understand, heres the code to my calculator, it might help.
while True:
calculation = input("Calculation: ")
if (calculation.__contains__("+")):
print(float(calculation[0] ) + float(calculation[2]))
elif (calculation.__contains__("-")):
print(float(calculation[0] ) - float(calculation[2]))
elif (calculation.__contains__("*")):
print(float(calculation[0] ) * float(calculation[2]))
elif (calculation.__contains__("/")):
print(float(calculation[0] ) / float(calculation[2]))
A float is a floating-point number (more or less your "basically 2 numbers with a dot in the middle").
The term double is short for "double precision floating-point number": a similar kind of number but typically using more bits to store it, allowing for more precision.
In Python, the type float is used to refer to all floating-point numbers, regardless of precision.
You mention Python's float and also double. These are exactly the same thing, because what Python calls float (pedantically, in most implementations of Python) is what everyone else calls double. And what C and C++ call float does not exist in Python.
I am trying to return a number with 6 decimal places, regardless of what the number is.
For example:
>>> a = 3/6
>>> a
0.5
How can I take a and make it 0.500000 while preserving its type as a float?
I've tried
'{0:.6f}'.format(a)
but that returns a string. I'd like something that accomplishes this same task, but returns a float.
In memory of the computer, the float is being stored as an IEEE754 object, that means it's just a bunch of binary data exposed with a given format that's nothing alike the string of the number as you write it.
So when you manipulate it, it's still a float and has no number of decimals after the dot. It's only when you display it that it does, and whatever you do, when you display it, it gets converted to a string.
That's when you do the conversion to string that you can specify the number of decimals to show, and you do it using the string format as you wrote.
This question shows a slight misunderstanding on the nature of data types such as float and string.
A float in a computer has a binary representation, not a decimal one. The rendering to decimal that python is giving you in the console was converted to a string when it was printed, even if it's implicit by the print function. There is no difference between how a 0.5 and 0.5000000 is stored as a float in its binary representation.
When you are writing application code, it is best not to worry about the presentation until it gets to the end user where it must, somehow, be converted to a string if only implicitly. At that point you can worry about decimal places, or even whether you want it shown in decimal at all.
So I have a list of tuples of two floats each. Each tuple represents a range. I am going through another list of floats which represent values to be fit into the ranges. All of these floats are < 1 but positive, so precision matter. One of my tests to determine if a value fits into a range is failing when it should pass. If I print the value and the range that is causing problems I can tell this much:
curValue = 0.00145000000671
range = (0.0014500000067055225, 0.0020968749796738849)
The conditional that is failing is:
if curValue > range[0] and ... blah :
# do some stuff
From the values given by curValue and range, the test should clearly pass (don't worry about what is in the conditional). Now, if I print explicitly what the value of range[0] is I get:
range[0] = 0.00145000000671
Which would explain why the test is failing. So my question then, is why is the float changing when it is accessed. It has decimal values available up to a certain precision when part of a tuple, and a different precision when accessed. Why would this be? What can I do to ensure my data maintains a consistent amount of precision across my calculations?
The float doesn't change. The built-in numberic types are all immutable. The cause for what you're observing is that:
print range[0] uses str on the float, which (up until very recent versions of Python) printed less digits of a float.
Printing a tuple (be it with repr or str) uses repr on the individual items, which gives a much more accurate representation (again, this isn't true anymore in recent releases which use a better algorithm for both).
As for why the condition doesn't work out the way you expect, it's propably the usual culprit, the limited precision of floats. Try print repr(curVal), repr(range[0]) to see if what Python decided was the closest representation of your float literal possible.
In modern day PC's floats aren't that precise. So even if you enter pi as a constant to 100 decimals, it's only getting a few of them accurate. The same is happening to you. This is because in 32-bit floats you only get 24 bits of mantissa, which limits your precision (and in unexpected ways because it's in base2).
Please note, 0.00145000000671 isn't the exact value as stored by Python. Python only diplays a few decimals of the complete stored float if you use print. If you want to see exactly how python stores the float use repr.
If you want better precision use the decimal module.
It isn't changing per se. Python is doing its best to store the data as a float, but that number is too precise for float, so Python modifies it before it is even accessed (in the very process of storing it). Funny how something so small is such a big pain.
You need to use a arbitrary fixed point module like Simple Python Fixed Point or the decimal module.
Not sure it would work in this case, because I don't know if Python's limiting in the output or in the storage itself, but you could try doing:
if curValue - range[0] > 0 and...