Gra
Gra

Reputation: 1594

Can floating point numbers ending with `.0` be considered as mathematical integers

We all know that using floating point numbers can lead to strange results:

>>> 0.1*3
0.30000000000000004

My question: can all floating point numbers ending with .0 be considered as mathematical integers?

My use case:

>>> x = 3.4296875
>>> 2*x*(128*x-695)+1856
100.0

Is it possible to assert that there is no quirk and that the result is a true and precise 100? I mean without doing the calculation by hand!

Thanks!

Upvotes: 0

Views: 568

Answers (2)

Eric Postpischil
Eric Postpischil

Reputation: 223747

There are several possible interpretations to this question or variant questions that might be asked, so I have listed some and addressed them individually.

If the fractional portion of a floating-point number is zero, does the number represent a mathematical integer?

Yes. Per the IEEE-754 floating-point standard, and commonly with other floating-point formats, a floating-point object represents a single specific number (or an infinity or a NaN). It does not represent an interval of numbers. So, if a floating-point object is not a NaN or infinity, and its fractional portion is zero, then the value it represents is an integer.

When proofs are written about floating-point arithmetic, the fact that various values are integers is commonly used as part of a proof.

If the result of a floating-point calculation is an integer, would the same calculation performed with real-number arithmetic produce an integer?

Not in general. Due to limited precision and rounding, floating-point calculations and real-number calculations may differ by arbitrary amounts.

In specific cases, it may be possible to analyze the floating-point operations and derive statements about what the floating-point results imply about the real-number results.

If a floating-point number is displayed with “.0”, is it an integer?

Not necessarily. When floating-point numbers are formatted for display, they are commonly rounded to a limited number of decimal digits. This may conceal the exact value, and some numbers with non-zero fraction portions may be displayed with “.0”.

If a floating-point number is an integer, can it be used as an integer type?

This depends on the programming language being used. In strongly typed languages, no, the value remains a floating-point type. In scripting languages, it is somewhat common to have some flexibility in using floating-point results as integers.

In the code in the question, is the result an integer?

In the code shown in the question, there are no rounding errors. The value for x, 3.4296875, is exactly 439/128, and this is representable without error in all common floating-point formats. So multiplying it by 128 produces an exact result, 439. Subtracting 695 from that produces −256, and multiplying 2*x*-256 produces exactly −1756, and then adding 1856 produces exactly 100. It is an integer. This may not be true in other cases where non-integer data is used.

Upvotes: 1

gee
gee

Reputation: 94

Floating point arithmetic is not a quirk magical mess. It is a precise arithmetic (after all, it is a machine which deals with it, not an intoxicated leprechaun). It is just much more complicated or rather much more unknown, than natural numbers or real numbers arithmetic. So short or long, the answer is no. Even Python, which looks sometimes (too) cool concerning types, is aware of this:

>>> x = 1+1e-100
>>> x == 1
True
>>> isinstance(x,int)
False
>>> isinstance(x,float)
True

The work of William Kahan has been pedagogical enough to make us being aware of all the dangers of tempting short paths.

Upvotes: 0

Related Questions