Lisp HUG Maillist Archive

approximation in float operations

Hello,

Is it normal that a simple addition of floats like (+ 0.1 0.6) returns 0.70000005 in LW ?

Is there something we can do about it ?

Thanks
Jean 



_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Re: approximation in float operations

Do about what? The inaccuracy comes from either the reader or the printer (or both); the fact is that in general it is not possible -- regardless of the programming language -- to represent decimals exactly in binary. This is most glaringly obvious if the decimal is something short and snappy like 0.7. 

Maybe you should use cl:format to control the number of decimal places printed? 

- nick

> On 25 Feb 2016, at 08:27, Jean Bresson <Jean.Bresson@ircam.fr> wrote:
> 
> 
> Hello,
> 
> Is it normal that a simple addition of floats like (+ 0.1 0.6) returns 0.70000005 in LW ?
> 
> Is there something we can do about it ?
> 
> Thanks
> Jean 
> 
> 
> 
> _______________________________________________
> Lisp Hug - the mailing list for LispWorks users
> lisp-hug@lispworks.com
> http://www.lispworks.com/support/lisp-hug.html
> 

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Re: approximation in float operations

Am 25/02/16 um 08:27 schrieb Jean Bresson:
 >
 > Hello,
 >
 > Is it normal that a simple addition of floats like (+ 0.1 0.6) 
returns 0.70000005 in LW ?

yes, see: http://floating-point-gui.de/

 > Is there something we can do about it ?

yes.

CL-USER 1 > (+ 1/10 6/10)
7/10

~jens

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Re: approximation in float operations

Here's a useful paper that deserves to be better known: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.22.6768 ("What Every Computer Scientist Should Know About Floating Point Arithmetic (1991)")

On Thu, Feb 25, 2016 at 8:36 AM, Nick Levine <nick@nicklevine.org> wrote:

Do about what? The inaccuracy comes from either the reader or the printer (or both); the fact is that in general it is not possible -- regardless of the programming language -- to represent decimals exactly in binary. This is most glaringly obvious if the decimal is something short and snappy like 0..7.

Maybe you should use cl:format to control the number of decimal places printed?

- nick

> On 25 Feb 2016, at 08:27, Jean Bresson <Jean.Bresson@ircam.fr> wrote:
>
>
> Hello,
>
> Is it normal that a simple addition of floats like (+ 0.1 0.6) returns 0.70000005 in LW ?
>
> Is there something we can do about it ?
>
> Thanks
> Jean
>
>
>
> _______________________________________________
> Lisp Hug - the mailing list for LispWorks users
> lisp-hug@lispworks.com
> http://www.lispworks.com/support/lisp-hug.html
>

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Re: approximation in float operations

You've seen the effects of computation with float numbers.

Two things you can do:

* compute with higher accuracy, for example using double floats instead of single floats:

CL-USER 2 > *READ-DEFAULT-FLOAT-FORMAT*
SINGLE-FLOAT

CL-USER 3 > (+ 0.1 0.6)
0.70000005

CL-USER 4 > (+ 0.1d0 0.6d0)
0.7D0

CL-USER 5 > (setf *READ-DEFAULT-FLOAT-FORMAT* 'double-float)
DOUBLE-FLOAT

CL-USER 6 > (+ 0.1 0.6)
0.7

But this makes it having higher accuracy, not making it exact.

With floating point numbers you may say that they are equal, when the difference is small than some epsilon...


* compute with exact numbers like integers or ratios. Common Lisp provides number types like integer, ratio and rational.

CL-USER 7 > (= (+ 1/10 6/10) 7/10)
T





Regards,

Rainer Joswig


> Am 25.02.2016 um 08:27 schrieb Jean Bresson <Jean.Bresson@ircam.fr>:
> 
> 
> Hello,
> 
> Is it normal that a simple addition of floats like (+ 0.1 0.6) returns 0.70000005 in LW ?
> 
> Is there something we can do about it ?
> 
> Thanks
> Jean 
> 
> 
> 
> _______________________________________________
> Lisp Hug - the mailing list for LispWorks users
> lisp-hug@lispworks.com
> http://www.lispworks.com/support/lisp-hug.html


_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Re: approximation in float operations

I usually wrap the GNU MPFR Library to do exact math.
http://www.mpfr.org

--maru



On Thu, Feb 25, 2016 at 5:05 PM, Jean Bresson <Jean.Bresson@ircam.fr> wrote:

Thank you Nick. ok sorry my question was a bit silly. Actually I think my problem is not just about printing but also avoiding such thing as  (= (+ 0.1 0.6) 0.7) => NIL
I was wondering if some easy workarounds existed for that.

jean


> On 24 Feb 2016, at 23:36, Nick Levine <nick@nicklevine.org> wrote:
>
> Do about what? The inaccuracy comes from either the reader or the printer (or both); the fact is that in general it is not possible -- regardless of the programming language -- to represent decimals exactly in binary. This is most glaringly obvious if the decimal is something short and snappy like 0.7.
>
> Maybe you should use cl:format to control the number of decimal places printed?
>
> - nick
>
>> On 25 Feb 2016, at 08:27, Jean Bresson <Jean.Bresson@ircam.fr> wrote:
>>
>>
>> Hello,
>>
>> Is it normal that a simple addition of floats like (+ 0.1 0.6) returns 0.70000005 in LW ?
>>
>> Is there something we can do about it ?
>>
>> Thanks
>> Jean
>>
>>
>>
>> _______________________________________________
>> Lisp Hug - the mailing list for LispWorks users
>> lisp-hug@lispworks.com
>> http://www.lispworks.com/support/lisp-hug.html
>>
>


_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Re: approximation in float operations

If you really require higher precision float arithmetic, you could use
external libraries. To avoid comparison problems, you can use a small
DELTA value as the range, instead of using Œ=Œ.

-Rangarajan

On 25/02/16 1:35 pm, "Jean Bresson" <Jean.Bresson@ircam.fr> wrote:

>
>Thank you Nick. ok sorry my question was a bit silly. Actually I think my
>problem is not just about printing but also avoiding such thing as  (= (+
>0.1 0.6) 0.7) => NIL
>I was wondering if some easy workarounds existed for that.
>
>jean
>
>
>> On 24 Feb 2016, at 23:36, Nick Levine <nick@nicklevine.org> wrote:
>> 
>> Do about what? The inaccuracy comes from either the reader or the
>>printer (or both); the fact is that in general it is not possible --
>>regardless of the programming language -- to represent decimals exactly
>>in binary. This is most glaringly obvious if the decimal is something
>>short and snappy like 0.7.
>> 
>> Maybe you should use cl:format to control the number of decimal places
>>printed? 
>> 
>> - nick
>> 
>>> On 25 Feb 2016, at 08:27, Jean Bresson <Jean.Bresson@ircam.fr> wrote:
>>> 
>>> 
>>> Hello,
>>> 
>>> Is it normal that a simple addition of floats like (+ 0.1 0.6) returns
>>>0.70000005 in LW ?
>>> 
>>> Is there something we can do about it ?
>>> 
>>> Thanks
>>> Jean 
>>> 
>>> 
>>> 
>>> _______________________________________________
>>> Lisp Hug - the mailing list for LispWorks users
>>> lisp-hug@lispworks.com
>>> http://www.lispworks.com/support/lisp-hug.html
>>> 
>> 
>
>
>_______________________________________________
>Lisp Hug - the mailing list for LispWorks users
>lisp-hug@lispworks.com
>http://www.lispworks.com/support/lisp-hug.html
>



_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Updated at: 2020-12-10 08:32 UTC