top | item 22861233

(no title)

dkulchenko | 5 years ago

That's not unique to JS.

  $ irb
  irb(main):001:0> 0.1 + 0.2
  => 0.30000000000000004
  irb(main):002:0>


  $ iex
  Erlang/OTP 22 [erts-10.6.2] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [hipe]

  Interactive Elixir (1.10.2) - press Ctrl+C to exit (type h() ENTER for help)
  iex(1)> 0.1 + 0.2
  0.30000000000000004
  iex(2)>

  $ python
  Python 2.7.17 (default, Dec  2 2019, 13:23:33)
  [GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.33.12)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.
  >>> 0.1 + 0.2
  0.30000000000000004
  >>>

discuss

order

airstrike|5 years ago

I know, it was just a tongue-in-cheek comment but oh well

yellowapple|5 years ago

Interestingly, though, some languages get it right:

    northrup@Topaz:~/Desktop$ perl -de1
    
    Loading DB routines from perl5db.pl version 1.55
    Editor support available.
    
    Enter h or 'h h' for help, or 'man perldebug' for more help.
    
    main::(-e:1): 1
      DB<1> say 0.1 + 0.2
    0.3
[…]

    northrup@Topaz:~$ csi
    CHICKEN
    (c) 2008-2019, The CHICKEN Team
    (c) 2000-2007, Felix L. Winkelmann
    Version 5.1.0 (rev 8e62f718)
    linux-unix-gnu-x86-64 [ 64bit dload ptables ]
    
    #;1> (+ 0.1 0.2)
    0.3
[…]

    northrup@Topaz:~$ sbcl
    This is SBCL 1.5.8, an implementation of ANSI Common Lisp.
    More information about SBCL is available at <http://www.sbcl.org/>.
    
    SBCL is free software, provided as is, with absolutely no warranty.
    It is mostly in the public domain; some portions are provided under
    BSD-style licenses.  See the CREDITS and COPYING files in the
    distribution for more information.
    * (+ 0.1 0.2)
    0.3

reitzensteinm|5 years ago

Unless they're using an alternate exact representation for floats by default, that's just rounding them when printing.

bbv-if|5 years ago

I have burnt myself once with perl: numbers that are printed the same are not necessarily equal.

  DB<1> say((0.1+0.2) != 0.3)
  1

jolmg|5 years ago

I don't think it's so much a matter of getting it right as much as it is about preferring the performance gain and ease of use of hardware-accelerated constant-space floating-point numbers. At least, that was probably the case when programming in something like C/C++. I don't imagine there's much point to it in ruby or python for example, other than that floating-point is standard by now.

voldacar|5 years ago

Comparing lisps with their full numeric towers to run of the mill procedural languages is a little unfair ;)