Floating point 101. And it almost never matters.
You say that, and then an army of idiots out in the real world continues to use floats for financial data and other large integers. I ran into a site that broke because they were using 64b unix nanotime in Javascript and comparing values which were truncated. You see this in js, python, etc. constantly.
9999999999999999.0 – 9999999999999998.0
71–80 of 83 posts
Re: 9999999999999999.0 – 9999999999999998.0
#72Floating point 101. And it almost never matters.
> And it almost never matters. I take issue with this. Drift from floating point inaccuracies can compound quickly and dramatically affect results. Sure if you're just looping over a 1000 item list, it's not going to matter that JavaScript is representing that as a float/double, but in a wide variety of contexts, such as anything to do with money, it absolutely does matter.
Re: 9999999999999999.0 – 9999999999999998.0
#73Earlier quoted context omitted.
I consider myself an old unix person, but I use dc. Possibly to make it more confusing to anyone who looks at what I'm doing. It also gives the math result here of course. % dc 9999999999999999.0 9999999999999998.0 - p 1.0 %
rpn for for the win I guess ;-). I actually didn't get to try my first linux stuff until grad school in the 80s. I wish I had saved off all my init files from then to see what they look like now.
I used bc's compiled output ("bc -c") to learn how to make dc jump through hoops.
Re: 9999999999999999.0 – 9999999999999998.0
#74bc -l is my standard command-line tool for arbitrary precision calculations (-l not needed here but handy for functions like sqrt(), e(), and log l() ): $ bc -l bc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 9999999999999999.0 - 9999999999999998.0 1.0
I got tired of typing bc -l years ago, I have a function so I can do it on the command line: calc 2^27 + 3^15. I wish somehow the shell would let me add parens here, instead of having to escape them with a quote. function calc2 { if [ $# -eq 0 ]; then echo 'pass commands for math evaluation, like calc l(2^32/17) + 3'; fi; echo $* | bc -l; }
= () { printf "%'.f\n" $(echo $1 | bc); }; alias calc==
Re: 9999999999999999.0 – 9999999999999998.0
#75Earlier quoted context omitted.
who does not use bc? people who aren't old unix people ;-) I use bc
I consider myself an old unix person, but I use dc. Possibly to make it more confusing to anyone who looks at what I'm doing. It also gives the math result here of course. % dc 9999999999999999.0 9999999999999998.0 - p 1.0 %
Re: 9999999999999999.0 – 9999999999999998.0
#76Moral of the story: we all should switch to programming in Perl6.
$ raku -e 'say 9999999999999999.0 - 9999999999999998.0'
1
https://raku.orgRe: 9999999999999999.0 – 9999999999999998.0
#77 with Ada.Text_IO; use Ada.Text_IO;
procedure Example is
begin
Put_Line (Float (9999999999999999.0-9999999999999998.0)'Image);
end Example;
Result: 1.00000E+00
If I really wanted to, I could use a decimal instead, e.g. with Ada.Text_IO; use Ada.Text_IO;
procedure Example is
type Decimal is delta 1.0E-20 digits 38;
begin
Put_Line (Decimal (9999999999999999.0-9999999999999998.0)'Image);
end Example;
Result: 1.00000000000000000000Re: 9999999999999999.0 – 9999999999999998.0
#78Re: 9999999999999999.0 – 9999999999999998.0
#79Emacs Calculator with high precision will get you what you need (`M-x calc RET P 20 RET 9999999999999999.0 RET 9999999999999998.0 RET -` gives you `1.`) Definitely keeping this example in my back pocket for explaining to people why floating point is not what you think it is.