[prev in list] [next in list] [prev in thread] [next in thread]
List: sbcl-help
Subject: Re: [Sbcl-help] SBCL double float vs C double float
From: Stas Boukarev <stassats () gmail ! com>
Date: 2019-06-23 19:05:38
Message-ID: CAF63=11Xfwb7hj6NtD+ut7X+X7FoHi1N7MCS3caeq10fxviN5w () mail ! gmail ! com
[Download RAW message or body]
You need to consistently use double floats throughout, not single floats.
On Sun, Jun 23, 2019 at 11:42 AM Aaron Chen <aaronchen.lisp@gmail.com> wrote:
>
> Hi,
> I want to ask why SBCL's double float different from C's double float during \
> calculation. I will post lisp code and c code below. I guess the code is the same.
> test.lisp :
> (defun test ()
> (flet ((f (x)
> (/ (floor (expt 2d0 (- 29.89 (* x x)))) #.(expt 10 9d0))))
> (loop repeat 55
> for u = (f 2.2) then (f u)
> finally (format t "~,9f" u))))
> (test)
> I get 0.704978697 as result in lisp.
>
> test.c :
> #include <math.h>
> #include <stdio.h>
>
> double F(double n)
> {
> double e = 29.89 - n*n;
> double power1 = floor(powl(2.0L, e));
> double power2 = 1e9;
> return power1 / power2;
> }
>
> int main()
> {
> double next=2.2;
> for(long i=1; i<=55; i++)
> {
> next = F(next);
> }
> printf("%.9f", next);
> return 0;
> }
> After running
> gcc test.c -o test -lm && ./test
> I get 0.704978874 as result in C.
>
> I try running the lisp code in CLISP/CCL, and show the comparison as follows,
> C 0.704978874
> SBCL 0.704978697
> CCL 0.704978697
> CLISP 0.704977280
>
> Why would they get different results? I want SBCL get the same result as C. How?
> _______________________________________________
> Sbcl-help mailing list
> Sbcl-help@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/sbcl-help
_______________________________________________
Sbcl-help mailing list
Sbcl-help@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sbcl-help
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic