Float

Rustom Mody rustompmody at gmail.com
Sat Jul 30 23:20:56 EDT 2016


On Saturday, July 30, 2016 at 5:21:44 PM UTC+5:30, Chris Angelico wrote:
> On Sat, Jul 30, 2016 at 9:44 PM, Cai Gengyang  wrote:
> > You mentioned that : A floating point number[2] is number that is not an integer (and not a
> > complex number)
> >
> > Hence ,
> >
> > 10 is not a floating point number because it is an integer
> > 25 is not a floating point number because it is an integer
> > 7 + 3i is not a floating number because it is a complex number
> > 8 + 5i is not a floating number because it is a complex number.
> >
> > Is 3.0 a floating number ? It is a rational number, not an integer right ?

As Steven pointed out the distinction you need to get is floating vs fixed
Scientific notation is a good analogy to understand.

3 looks smaller than 30000000000000000000000
Not so much in scientific notation:

>>> "%g" % 3
'3'
>>> "%g" % 300000000000000000000
'3e+20'
>>> 
In effect the position of the decimal point is captured but not the presumably
non-significant digits

Note this is an analogy.  Unfortunately in practice…
> 
> In a computing context, data types are incredibly significant.

…in practice data types and in particular numeric data types in most
mainstream languages is a total mess.
And becoming an effective programmer means learning to live with/around it.

Some indications of the mess:
>>> .1 + .1 == .2
True
>>> .1 + .1 + .1== .3
False

To get a clue why:
>>> .5 . as_integer_ratio()
(1, 2)   # ie ½
>>> .25 . as_integer_ratio()
(1, 4)  # ie ¼
>>> .1 . as_integer_ratio()
(3602879701896397, 36028797018963968)     # ie OOOOPPPS!!

Another (and in my opinion bigger) violation that most programming languages
including python make over standard math is that fundamental subset relations
are brazenly violated and then duct-taped-over with something called časting’

ie in math we have
ℕ ⊂ ℤ ⊂ ℚ ⊂ ℝ

In programming float, int are all disjoint
That disjointness is bandaided with casts
These should be in principle reversible

>>> x = 10
>>> float(x)
10.0
>>> int(float(x))
10
>>> int(float(x)) == x
True

# So far so good

>>> y = 1000000000000000000000000000000000000000000000000000000000000000000000
>>> float(y)
1e+69
>>> int(float(y)) == y
False

# WTF?! Lets see why...

>>> int(float(y))
1000000000000000072531436381529235126158374409646521955518210155479040L

tl;dr Donald Knuth, is considered one of the greatest programmers. Its a good 
idea to follow his example.

In writing Tex he went out of his way to implement his own fixed point
system and avoid using the builtin hardware floating point
https://en.wikipedia.org/wiki/TeX#Development

To the extent its feasible it’s advisable to follow the example of Knuth
[No not writing your own system, but avoiding when/if possible]



More information about the Python-list mailing list