Menu

#4604 Floating point errors

None
not-a-bug
nobody
None
5
2025-09-02
2025-09-01
No

The answer to 10.0/3 or float(10/3) is 3.3333333333333335 instead of 3.333333333333333

(%i1) 10.0/3;
(%o1)                         3.3333333333333335
(%i2) float(10/3);
(%o2)                         3.3333333333333335
(%i3) float(100/3);
(%o3)                         33.333333333333336
(%i4) float(1000/3);
(%o4)                          333.3333333333333
(%i5) float(10000/3);
(%o5)                         3333.3333333333335

Please note in all cases but 1000/3 there are 17 printed digits instead of 16. However setting fpprintprec to 16 does not completely solve the problem, as float(100/3) is still wrong

(%i6) fpprintprec:16;
(%o6)                                 16
(%i7) 10.0/3;
(%o7)                          3.333333333333333
(%i8) float(10/3);
(%o8)                          3.333333333333333
(%i9) float(100/3);           
(%o9)                          33.33333333333334
(%i10) float(1000/3);
(%o10)                         333.3333333333333
(%i11) float(10000/3);
(%o11)                         3333.333333333333

I use maxima 5.48.1 in Archlinux

Related

Bugs: #4604

Discussion

  • David Billinghurst

    • status: open --> not-a-bug
     
  • David Billinghurst

    This is not a bug. It is the expected behavior of floating point arithmetic on any computer.

    maxima has a number of ways to address the issue you have reported. I suggest you ask specific questions on the Maxima discussion list

    You can refer to What every computer scientist should know about floating-point arithmetic for some background.

     
  • Raymond Toy

    Raymond Toy - 2025-09-01

    In addition to what David said, there is a subtle difference between 10.0/3 and float(10/3). The former is a floating-point division between 10.0 and 3.0. Both of thsee can be represented exactly in floating point. However, float(10/3) is the nearest floating-point value to the rational 10/3. These two values aren't always the same due to how rounding is done.

    You can check the value by doing something like :lisp (print (/ 10.0 3)). For me with cmucl, I get 3.3333333333333335. I know cmucl uses an algorithm that prints a value that when read in produces the same value as produced by (/ 10.0 3). I'm pretty sure sbcl uses the same algorithm. I believe all other lisps use the similar algorithms that produce the same output such that when read in, the exact same floating-point value is produced.

     
    • Stavros Macrakis

      We need to write a FAQ about floating point. Of course it is the same in
      every other system, but Maxima also has bigfloats and rationals, which are
      helpful for discussing floats. For example:

      rationalize -- converts a floating point number to the exact rational
      number it is equal to -- the denominator is always a power of 2

      rationalize(10.0/3.0) - 1/3 => 1/6755399441055744 ... float(%)
      => 1.4802973661668753e-16

      rat -- finds the "nicest" rational within ratepsilon (default 2.0e-15) of a
      float

      10.0/3.0 => 3.3333333333333335 ... rat(%)
      rat: replaced 3.3333333333333335 by 10/3 = 3.3333333333333335
      => 1/3

      cf -- finds the continued fraction representation -- shows the "nicest"
      nearby rationals
      cfdisrep -- converts to standard arithmetic form

      cfdisrep(cf(10.0./3.0)) => 3+1/(2+1/(1+1/750599937895082))

      On Mon, Sep 1, 2025 at 1:44 PM Raymond Toy rtoy@users.sourceforge.net
      wrote:

      In addition to what David said, there is a subtle difference between
      10.0/3 and float(10/3). The former is a floating-point division between
      10.0 and 3.0. Both of thsee can be represented exactly in floating point.
      However, float(10/3) is the nearest floating-point value to the rational
      10/3. These two values aren't always the same due to how rounding is done.

      You can check the value by doing something like :lisp (print (/ 10.0 3)).
      For me with cmucl, I get 3.3333333333333335. I know cmucl uses an algorithm
      that prints a value that when read in produces the same value as produced
      by (/ 10.0 3). I'm pretty sure sbcl uses the same algorithm. I believe
      all other lisps use the similar algorithms that produce the same output
      such that when read in, the exact same floating-point value is produced.


      [bugs:#4604] https://sourceforge.net/p/maxima/bugs/4604/ Floating point
      errors

      Status: not-a-bug
      Group: None
      Created: Mon Sep 01, 2025 08:07 AM UTC by David J. López
      Last Updated: Mon Sep 01, 2025 08:30 AM UTC
      Owner: nobody

      The answer to 10.0/3 or float(10/3) is 3.3333333333333335 instead of
      3.333333333333333

      (%i1) 10.0/3;
      (%o1) 3.3333333333333335
      (%i2) float(10/3);
      (%o2) 3.3333333333333335
      (%i3) float(100/3);
      (%o3) 33.333333333333336
      (%i4) float(1000/3);
      (%o4) 333.3333333333333
      (%i5) float(10000/3);
      (%o5) 3333.3333333333335

      Please note in all cases but 1000/3 there are 17 printed digits instead of
      16. However setting fpprintprec to 16 does not completely solve the
      problem, as float(100/3) is still wrong

      (%i6) fpprintprec:16;(%o6) 16(%i7) 10.0/3;(%o7) 3.333333333333333(%i8) float(10/3);(%o8) 3.333333333333333(%i9) float(100/3); (%o9) 33.33333333333334(%i10) float(1000/3);(%o10) 333.3333333333333(%i11) float(10000/3);(%o11) 3333.333333333333

      I use maxima 5.48.1 in Archlinux

      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/maxima/bugs/4604/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

       

      Related

      Bugs: #4604

  • David J. López

    David J. López - 2025-09-02

    Thank you for your quick and detailed answers, and for the article as well. Apologies for opening this ticket as a bug.

     
    • David Billinghurst

      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

      No problem.  Please follow up on the maxima-discuss list. 

      On 2/09/2025 18:04, David J. López via Maxima-bugs wrote:
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">

      Thank you for your quick and detailed answers, and for the article as well. Apologies for opening this ticket as a bug.


      [bugs:#4604] Floating point errors

      Status: not-a-bug
      Group: None
      Created: Mon Sep 01, 2025 08:07 AM UTC by David J. López
      Last Updated: Mon Sep 01, 2025 05:44 PM UTC
      Owner: nobody

      The answer to 10.0/3 or float(10/3) is 3.3333333333333335 instead of 3.333333333333333

      (%i1) 10.0/3;
      (%o1)                         3.3333333333333335
      (%i2) float(10/3);
      (%o2)                         3.3333333333333335
      (%i3) float(100/3);
      (%o3)                         33.333333333333336
      (%i4) float(1000/3);
      (%o4)                          333.3333333333333
      (%i5) float(10000/3);
      (%o5)                         3333.3333333333335
      

      Please note in all cases but 1000/3 there are 17 printed digits instead of 16. However setting fpprintprec to 16 does not completely solve the problem, as float(100/3) is still wrong

      (%i6) fpprintprec:16;
      (%o6)                                 16
      (%i7) 10.0/3;
      (%o7)                          3.333333333333333
      (%i8) float(10/3);
      (%o8)                          3.333333333333333
      (%i9) float(100/3);           
      (%o9)                          33.33333333333334
      (%i10) float(1000/3);
      (%o10)                         333.3333333333333
      (%i11) float(10000/3);
      (%o11)                         3333.333333333333
      

      I use maxima 5.48.1 in Archlinux


      Sent from sourceforge.net because maxima-bugs@lists.sourceforge.net is subscribed to https://sourceforge.net/p/maxima/bugs/

      To unsubscribe from further messages, a project admin can change settings at https://sourceforge.net/p/maxima/admin/bugs/options. Or, if this is a mailing list, you can unsubscribe from the mailing list.


      <fieldset class=""></fieldset>
      <fieldset class=""></fieldset>
      _______________________________________________
      Maxima-bugs mailing list
      Maxima-bugs@lists.sourceforge.net
      https://lists.sourceforge.net/lists/listinfo/maxima-bugs
      
       

Log in to post a comment.