create account

Floating point math is the devil! by edicted

View this thread on: hive.blogpeakd.comecency.com
· @edicted ·
$39.24
Floating point math is the devil!
https://www.youtube.com/watch?v=0cVlTeIATBs

#### Programming exercise:
Head on over to https://playcode.io/new/

### Type in:
    console.log(0.1 * 125)

<center>
![image.png](https://files.peakd.com/file/peakd-hive/edicted/Eo1vv2oohfhamyVz2qub8BQ5Uiu8dU7mB3rudLLHActJhJ5dKMG5yAXsjrEYJEDAcdU.png)</center>

### No problem!
Look at you go, you're a programmer now!

### Now add a zero!


![t1.png](https://files.peakd.com/file/peakd-hive/edicted/23t74pHR2m8UqfMkcumNRdpvjVNGZTKi5kamutn6QMFeuG5sYnG3qrLcwVzUMa2aPL2V4.png)


### Easy, add another 0.



![image.png](https://files.peakd.com/file/peakd-hive/edicted/23t74u1xAHvgUmiVg7ekj3QsubmdzUpwdh6JMkDcEtu4QZytJADpseYYNYqjtgER2fxMn.png)


### What's the point of this again? 
Pipe down: add two more zeros. 


![image.png](https://files.peakd.com/file/peakd-hive/edicted/Eo1vUTMLjo3468sM3yascBo5mDMDGdAKevS9KFesGxHx7935RWCa6Fx1SuXKcvJcXtt.png)

### Okay, this is getting ridiculous.
Ah, so close, just two more zeros to go!


![image.png](https://files.peakd.com/file/peakd-hive/edicted/23tGTqknVeqUcXJcLFX1kKtsoCQtHx6CDtZWKQ8f4manSZrCH7M5HzYgjWprRyZDd6h1J.png)

# WHAT?!?!
GAH! WHY!?

How does 0.0000001 x 125 = 0.0000124999999999???

The math can't get more basic, why the rounding error? 

### Short answer: floating point math. 




<center>
![float-math-point.png](https://files.peakd.com/file/peakd-hive/edicted/Eokb6Y325oDhbA836TZygoRgbKsQ5atnkoh1Dmmk9VD8ZBcCkxtAG2tKRSUGpRS6kjH.png)</center>

#### You and I are used to math being done in base 10. 
* There are ten digits. 
* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
* When multiplying or dividing by a power of 10, the decimal point moves. 

#### Computers do math in base 2. 
* There are two digits 
* on/off true/false 1/0
* When multiplying or dividing by a power of 2, the decimal point moves. 

So when you get into fractions, a computer can't store numbers in base two like we think of numbers in base ten.  For example, try to write 1/3 in base ten.  What do you get? 

>0.333333333333333333333... 

1/3 can not be expressed perfectly as a base 10 decimal.  If you were using a base 3 number system 1/3 would be 0.1 and could be expressed perfectly without a rounding error.  This same thing happens with floating point math to create some weird rounding errors (often times when multiplying/dividing numbers). 

https://floating-point-gui.de/formats/binary/

<center>![bored-kid.jpg](https://files.peakd.com/file/peakd-hive/edicted/242hjYzcykrDJYoGDS3b5T6MFNwNrQv3d6F2urbpoAsoLr8HcDq1u3zRv1g96Ew4GtXBY.jpg)</center>

### Who cares? 
I care!  DAMMIT! When you're trying to keep track of money in a database (I am) then these rounding errors become unacceptable and totally ruin database integrity. In the case of the bug I just found, I wasn't even trying to keep track of money, but rather have been working on my [sliding-scale voting system.](https://peakd.com/hive-167922/@edicted/hive-governance-tug-of-war-sliding-scale-voting) 

The schedule I've developed DEPENDS on the numbers being exact.  If a variable in the database gets voted up/down and becomes 0.00014999999 instead of 0.00015 my code would get very confused and stop working. 

This is exactly why every database has a DECIMAL class that completely avoids floating point math and actually uses base 10.  It's slower and requires more storage but the loss in performance is more than worth it to maintain database integrity in many cases (especially those involving money).  

In the case of my code, I was actually using a <code>DECIMAL</code> when the error occurred, but I was multiplying it by another exponent that was by default a floating point number.  In the background it must have cast the entire result to a float and thus there was a bug in my code. 

    return cast(power(10, deviation) as decimal(65,30)) * temp;

In order to fix the bug I had to <code>CAST</code> the exponent as a decimal in order to stop floating-point math from occurring.  Problem solved. 

### Conclusion
Well, that's how my day is going. 
Just grinding out more code. 
It's been slow going but hopefully I can pick up the pace. 

###### Market Watch Post Script:

<center>
![image.png](https://files.peakd.com/file/peakd-hive/edicted/23t77B46LJGyn6zbmhdAwHbUMTE8CRw7Pr5WeNsEsgpzEnWhf1CVhrE8vEZ7Fwss2n1Fm.png)
</center>

##### Bitcoin shorts on Bitfinex are at local record highs. 
Are the bears getting a bit too greedy?
Will there be an epic short squeeze? 
Or will this market cave in once again? 
Time will tell. 
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 392 others
properties (23)
authoredicted
permlinkfloating-point-math-is-the-devil
categoryhive-167922
json_metadata{"app":"leofinance/0.2","format":"markdown","tags":["hivedev","javascript","mysql","palnet","proofofbrain","leo","magitek","leofinance"],"canonical_url":"https://leofinance.io/@edicted/floating-point-math-is-the-devil","links":["https://www.youtube.com/watch?v=0cVlTeIATBs","https://playcode.io/new/","https://floating-point-gui.de/formats/binary/","https://peakd.com/hive-167922/@edicted/hive-governance-tug-of-war-sliding-scale-voting"],"image":["https://img.youtube.com/vi/0cVlTeIATBs/0.jpg","https://files.peakd.com/file/peakd-hive/edicted/Eo1vv2oohfhamyVz2qub8BQ5Uiu8dU7mB3rudLLHActJhJ5dKMG5yAXsjrEYJEDAcdU.png","https://files.peakd.com/file/peakd-hive/edicted/23t74pHR2m8UqfMkcumNRdpvjVNGZTKi5kamutn6QMFeuG5sYnG3qrLcwVzUMa2aPL2V4.png","https://files.peakd.com/file/peakd-hive/edicted/23t74u1xAHvgUmiVg7ekj3QsubmdzUpwdh6JMkDcEtu4QZytJADpseYYNYqjtgER2fxMn.png","https://files.peakd.com/file/peakd-hive/edicted/Eo1vUTMLjo3468sM3yascBo5mDMDGdAKevS9KFesGxHx7935RWCa6Fx1SuXKcvJcXtt.png","https://files.peakd.com/file/peakd-hive/edicted/23tGTqknVeqUcXJcLFX1kKtsoCQtHx6CDtZWKQ8f4manSZrCH7M5HzYgjWprRyZDd6h1J.png","https://files.peakd.com/file/peakd-hive/edicted/Eokb6Y325oDhbA836TZygoRgbKsQ5atnkoh1Dmmk9VD8ZBcCkxtAG2tKRSUGpRS6kjH.png","https://files.peakd.com/file/peakd-hive/edicted/242hjYzcykrDJYoGDS3b5T6MFNwNrQv3d6F2urbpoAsoLr8HcDq1u3zRv1g96Ew4GtXBY.jpg","https://files.peakd.com/file/peakd-hive/edicted/23t77B46LJGyn6zbmhdAwHbUMTE8CRw7Pr5WeNsEsgpzEnWhf1CVhrE8vEZ7Fwss2n1Fm.png"]}
created2021-06-09 18:44:30
last_update2021-06-09 18:44:30
depth0
children10
last_payout2021-06-16 18:44:30
cashout_time1969-12-31 23:59:59
total_payout_value23.528 HBD
curator_payout_value15.707 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,479
author_reputation2,886,784,358,162,635
root_title"Floating point math is the devil! "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,234,952
net_rshares71,759,515,593,857
author_curate_reward""
vote details (456)
@builderofcastles ·
$0.02
In computers, we have floating point math and integer math.
It is interesting that no one has done fractional math yet.
It would be a very big advantage for certain problem types.

But i also remember the days when we had an entire separate chip to do math coprocessing.
and it was mostly just a processor with registers that held floating point numbers.

And your example is why i still like C and like typed values.

To many of the modern languages will just convert types on the fly, but i really hate that, i would wish a warning about changing types.
👍  
properties (23)
authorbuilderofcastles
permlinkqug707
categoryhive-167922
json_metadata{"app":"hiveblog/0.1"}
created2021-06-09 18:56:06
last_update2021-06-09 18:56:06
depth1
children2
last_payout2021-06-16 18:56:06
cashout_time1969-12-31 23:59:59
total_payout_value0.012 HBD
curator_payout_value0.012 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length555
author_reputation166,531,717,303,733
root_title"Floating point math is the devil! "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,235,114
net_rshares71,528,296,772
author_curate_reward""
vote details (1)
@yintercept ·
One can do fractional math by recording the numerator and denominator separately. The problem is that the set of rational numbers is not closed over some math operations; So you have to avoid those operations. So, you really don't gain that much for all of the extra storage.

One can do fixed decimal math by remembering the location of the decimal place. I've worked on many systems that record every thing as pennies.

Of course there is a problem with every mathematical system we used because it is impossible to create a math system that is complete.
properties (22)
authoryintercept
permlinkqugh74
categoryhive-167922
json_metadata{"app":"hiveblog/0.1"}
created2021-06-09 22:30:42
last_update2021-06-09 22:30:42
depth2
children1
last_payout2021-06-16 22:30:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length556
author_reputation23,962,343,431,134
root_title"Floating point math is the devil! "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,238,460
net_rshares0
@builderofcastles ·
$0.06
Yes, with a programmable computer, you can get it to do any kind of math... the hard way.

But think of things like Autocad.  Having a 1/16 fractional system or a 1/12 fractional system would have made a lot of the math and  number storage easy.

Instead you have 0.0625 and 0.41667 stored everywhere.

And, as you said, it can be imitated.  And moving the decimal place to pennies, or ⅒ of pennies works well for those kinds of integers that deal with money.
👍  , ,
properties (23)
authorbuilderofcastles
permlinkqugj65
categoryhive-167922
json_metadata{"app":"hiveblog/0.1"}
created2021-06-09 23:18:51
last_update2021-06-09 23:18:51
depth3
children0
last_payout2021-06-16 23:18:51
cashout_time1969-12-31 23:59:59
total_payout_value0.032 HBD
curator_payout_value0.031 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length459
author_reputation166,531,717,303,733
root_title"Floating point math is the devil! "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,239,143
net_rshares183,392,126,527
author_curate_reward""
vote details (3)
@chasmic-cosm ·
$0.05
It gets even weirder!

I tried this in clojure...

```lisp
(* 0.000001 125) => 1.25E-4

(* 0.0000001 125) => 1.2499999999999999E-5
```

This is the same as your result.

But then go one step further:

```lisp
(* 0.00000001 125) => 1.25E-6
```

We are back to an accurate result!

But using the division operation instead of multiplication gives different accuracy:

```lisp
(/ 125 10E7) => 1.25E-6

(/ 125 10E6) => 1.25E-5
```

Floating point arithmetic is hilarious!

Posted Using [LeoFinance <sup>Beta</sup>](https://leofinance.io/@chasmic-cosm/re-edicted-4fkjsh)
👍  
properties (23)
authorchasmic-cosm
permlinkre-edicted-4fkjsh
categoryhive-167922
json_metadata{"app":"leofinance/0.2","format":"markdown","tags":["hive-167922","leofinance"],"canonical_url":"https://leofinance.io/@chasmic-cosm/re-edicted-4fkjsh"}
created2021-06-10 10:42:36
last_update2021-06-10 10:42:36
depth1
children0
last_payout2021-06-17 10:42:36
cashout_time1969-12-31 23:59:59
total_payout_value0.024 HBD
curator_payout_value0.023 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length565
author_reputation4,159,775,155,640
root_title"Floating point math is the devil! "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,247,180
net_rshares138,851,980,361
author_curate_reward""
vote details (1)
@chasmic-cosm ·
Languages that do symbolic maths, such as the Wolfram language, seem to do a bit better on this front: 

https://www.wolframalpha.com/input/?i=0.0000001+*+125

Posted Using [LeoFinance <sup>Beta</sup>](https://leofinance.io/@chasmic-cosm/re-edicted-l6gvu)
properties (22)
authorchasmic-cosm
permlinkre-edicted-l6gvu
categoryhive-167922
json_metadata{"app":"leofinance/0.2","format":"markdown","tags":["hive-167922","leofinance"],"canonical_url":"https://leofinance.io/@chasmic-cosm/re-edicted-l6gvu","links":["https://www.wolframalpha.com/input/?i=0.0000001+*+125"]}
created2021-06-10 10:47:03
last_update2021-06-10 10:47:03
depth1
children0
last_payout2021-06-17 10:47:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length255
author_reputation4,159,775,155,640
root_title"Floating point math is the devil! "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,247,222
net_rshares0
@empress-eremmy ·
$0.02
Well congratulations...my Brain officially does not compute...lol. Funny to me how I struggled to keep up with all that, and I thought I had a fair knowledge of programming ( at least python basics)

At least I can still wish you the very best of luck as the project comes along



---

<center><sub>Posted via [proofofbrain.io](https://www.proofofbrain.io/@empress-eremmy/qugbui)</sub></center>
👍  
properties (23)
authorempress-eremmy
permlinkqugbui
categoryhive-167922
json_metadata{"tags":["proofofbrain"],"app":"proofofbrain/0.1","canonical_url":"https://www.proofofbrain.io/@empress-eremmy/qugbui"}
created2021-06-09 20:35:09
last_update2021-06-09 20:35:09
depth1
children0
last_payout2021-06-16 20:35:09
cashout_time1969-12-31 23:59:59
total_payout_value0.012 HBD
curator_payout_value0.012 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length395
author_reputation472,110,043,957,566
root_title"Floating point math is the devil! "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,236,707
net_rshares71,506,386,764
author_curate_reward""
vote details (1)
@fabian98 ·
I didn't understand much, but I found it quite interesting.

Posted Using [LeoFinance <sup>Beta</sup>](https://leofinance.io/@fabian98/re-edicted-4gi4of)
properties (22)
authorfabian98
permlinkre-edicted-4gi4of
categoryhive-167922
json_metadata{"app":"leofinance/0.2","format":"markdown","tags":["hive-167922","leofinance"],"canonical_url":"https://leofinance.io/@fabian98/re-edicted-4gi4of"}
created2021-06-10 15:14:36
last_update2021-06-10 15:14:36
depth1
children0
last_payout2021-06-17 15:14:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length153
author_reputation311,071,999,420,395
root_title"Floating point math is the devil! "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,251,520
net_rshares0
@jfang003 ·
$0.02
Well floats are definitely annoying since they have those rounding errors due to the power of binary digits. All you can do is get an approximate and fix the number like you did using cast. 

Well I don't plan on shorting so either way I don't mind which way it goes. So long as it goes up in the long run.

Posted Using [LeoFinance <sup>Beta</sup>](https://leofinance.io/@jfang003/re-edicted-5xoyja)
👍  , , ,
properties (23)
authorjfang003
permlinkre-edicted-5xoyja
categoryhive-167922
json_metadata{"app":"leofinance/0.2","format":"markdown","tags":["hive-167922","leofinance"],"canonical_url":"https://leofinance.io/@jfang003/re-edicted-5xoyja"}
created2021-06-10 00:10:15
last_update2021-06-10 00:10:15
depth1
children0
last_payout2021-06-17 00:10:15
cashout_time1969-12-31 23:59:59
total_payout_value0.011 HBD
curator_payout_value0.005 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length400
author_reputation431,564,071,652,898
root_title"Floating point math is the devil! "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,239,861
net_rshares69,384,024,936
author_curate_reward""
vote details (4)
@klevn · (edited)
made me wonder what my fav language would handle this..

```bashShell
marc@vishnu ~> tclsh
% proc myAdd x {
expr { $x * 125 }
}
% myAdd 0.001
0.125
% myAdd 0.00000001
1.25e-6
```

tcl returned value in scientific notation to be correct, would cause formatting errors, no calc errors.

i love programming, have fun, keep light.
properties (22)
authorklevn
permlinkquhcwh
categoryhive-167922
json_metadata{"app":"hiveblog/0.1"}
created2021-06-10 09:55:33
last_update2021-06-10 09:59:45
depth1
children0
last_payout2021-06-17 09:55:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length326
author_reputation539,164,563,565
root_title"Floating point math is the devil! "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,246,599
net_rshares0
@young-boss-karin ·
$0.02
I didn't understand anything I just read until I got to the last 5 lines. Good luck with that mehn.

Posted Using [LeoFinance <sup>Beta</sup>](https://leofinance.io/@young-boss-karin/re-edicted-3tnxsz)
👍  
properties (23)
authoryoung-boss-karin
permlinkre-edicted-3tnxsz
categoryhive-167922
json_metadata{"app":"leofinance/0.2","format":"markdown","tags":["hive-167922","leofinance"],"canonical_url":"https://leofinance.io/@young-boss-karin/re-edicted-3tnxsz"}
created2021-06-09 20:11:36
last_update2021-06-09 20:11:36
depth1
children0
last_payout2021-06-16 20:11:36
cashout_time1969-12-31 23:59:59
total_payout_value0.012 HBD
curator_payout_value0.012 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length201
author_reputation97,272,211,701,023
root_title"Floating point math is the devil! "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,236,372
net_rshares71,519,580,753
author_curate_reward""
vote details (1)