Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2019-06-26 17:59:35

George,Y
Member
Registered: 2006-03-12
Posts: 1,379

Why float number is not as accurate as human thinks and the solution~

Recently I had a hard time to educate coders that float numbers cannot be used in finance because of lack of accuracy.

They are all educated in a prestigious university specializing in computer science.

However the good old fundamentals seem to have gone from the courses they have taken, and from the coding field in general.

What is a float number?

A binary number with a given bits allowed before the binary point and after it.

For example:

110.0
-0.00001

The former will transform to decimal as 8+4=12,
while the latter will transform to decimal as -1/32 or equivalent.

The integer part is fine from and to decimal 12 <-> 110
But the fractional part (less than 1) has a natural inaccuracy
In decimal if you have a finite fraction that could be some integer multiple of power of 1/10
0.4
or 0.44
you will find it is not possible to transform it to finite fraction on 1/2
0.4 = 0.25+0.125+(0.025) = 0.25+0.125 + 0.015625+0.0078125 +(0.0015625)
-> 0.01100110011...

Of course the computer will simply cut off from some digits after the point

Thus you if you type in a float 0.4 into the computer, it looks as if normal 0.4,
but it will show off inaccuracy at the long end if you multiply a simple integer to it, say 3.
Because the programming language knows it will generally be inaccurate and usually round the last digit before presentation.
As you magnify the discrepancy by the multiplication, the programming language tells the little secret off.

The solution?
Almost all programming language and traditional database have a "decimal" number type.
A "decimal" number is actually two - the integer of the the number string, and the number of figures after the decimal point.
3.14 = Dec(314,2)
Thus the decimal is perfect for financial calculations to store, plus, minus or multiply.


X'(y-Xβ)=0

Offline

#2 2020-04-18 16:05:12

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Why float number is not as accurate as human thinks and the solution~

Yeah, bobbym used to be pretty obsessed with all the nuances behind floating point computations.

I think being able to represent arithmetic with real numbers in a precise way is a topic of current research. One person I know of, who does that sort of thing is Eva Darulova in MPI-SWS


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#3 2021-08-12 00:07:02

kevinvholloman
Member
From: Chicago, Illinois
Registered: 2021-08-11
Posts: 1

Re: Why float number is not as accurate as human thinks and the solution~

It would be interesting to read in an accessible form how mathematical operations are performed with float numbers in this form and how you can efficiently convert a number from an ordinary string to a float.

Offline

Board footer

Powered by FluxBB