This was a question in an exam but no clear explanation why. It was a true/false type question with;
There exists a value x of the type float for that holds: x + 1 == x...
Which is true. Why tho?
I guess it has to do with type conversion? But I can't imagine how that would work.
Eight digits are used to represent a floating point number : two for the exponent and six for the mantissa. The sign of the mantissa will be represented as + or -, but in the computer it is represented by a bit: 1 means negative, 0 means positive.
A signed floating point number. Either a decimal point (.) or an exponent symbol (e) must be present within the number for it to be a valid float.
Over the entire typical float range, about 232 different values can be represented.
Float is used to represent real numbers and is written with a decimal point dividing the integer and fractional parts. For example, 97.98, 32.3+e18, -32.54e100 all are floating point numbers. Python float values are represented as 64-bit double-precision values. The maximum value any floating-point number can be is approx 1.8 x 10 308.
Values that the float() method can return depending upon the argument passed. If an argument is passed, then the equivalent floating point number is returned. If no argument is passed then the method returns 0.0 . If any string is passed that is not a decimal point number or does not match to any cases mentioned above then an error will be raised.
The maximum value any floating-point number can be is approx 1.8 x 10 308. Any number greater than this will be indicated by the string inf in Python. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
The float() method is used to return a floating point number from a number or a string. Syntax: float(x) The method only accepts one parameter and that is also optional to use. Let us look at the various types of argument, the method accepts: A number : Can be an Integer or a floating point number.
Sure.
#include <limits>
#include <iostream>
int main() {
float f = std::numeric_limits<float>::infinity();
std::cout << (f == f + 1) << std::endl;
}
As Deduplicator points out, if your float
is big enough (works for me with float f = 1e20;
), it'll also work because the added 1
would be outside of the float
's accuracy.
Try it online
This code compiles without error:
#include <limits>
int main()
{
static_assert(std::numeric_limits<float>::infinity() == std::numeric_limits<float>::infinity() + 1.0f, "error");
static_assert(std::numeric_limits<double>::infinity() == std::numeric_limits<double>::infinity() + 1.0, "error");
return 0;
}
online version
You don't need to even use infinity. If the number is big enough, rounding errors become big enough so adding one to the number doesn't change it at all. E.g.
static_assert(100000000000000000000000.f == 100000000000000000000000.f + 1.0, "error");
The specific number of 0
you have to put here may be implementation defined, though.
Always keep rounding in mind when you write programs that use floating point numbers.
#include <iostream>
int main()
{
float val = 1e5;
while (val != val + 1)
val++;
std::cout << val << "\n";
return 1;
}
Prints 1.67772e+07
for clang.
There exists a value x of the type float for that holds: x + 1 == x... which is true. Why tho?
The reason for that lies in how floating point numbers work. Basically a 32bit float has 24 bits for the mantissa (the base digits) and 8 bits for the exponent. At some point +1 just doesn't cause a change in the binary representation because the exponent is too high.
For a 32-bit IEEE754 float (a.k.a. single-precision or SP), the minumum non-negative normal such value is 16777216
. That is, 16777216 + 1 == 16777216
.
The number 16777216
is precisely 2^24
. An SP float has 23 bits of mantissa. This is how it is represented internally:
3 2 1 0
1 09876543 21098765432109876543210
S ---E8--- ----------F23----------
Binary: 0 10010111 00000000000000000000000
Hex: 4B80 0000
Precision: SP
Sign: Positive
Exponent: 24 (Stored: 151, Bias: 127)
Hex-float: +0x1p24
Value: +1.6777216e7 (NORMAL)
You can see the entire mantissa is 0. If you add 1 to this number, it'll hit the gap and be absorbed, giving you back precisely what you started with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With