Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The Common Lisp answer is 0.3 and not 0.30000000000000004 because those are single float literals: for double float literals, we have (+ 0.1d0 0.2d0) => 0.30000000000000004d0.

I would imagine this is the case for several of the other examples, too.



You can tell Common Lisp to read double-floats by default.

    CL-USER 113 > (+ 0.1 0.2)
    0.3

    CL-USER 114 > *read-default-float-format*
    SINGLE-FLOAT

    CL-USER 115 > (setf *read-default-float-format* 'double-float)
    DOUBLE-FLOAT

    CL-USER 116 > (+ 0.1 0.2)
    0.30000000000000005
Thus if one wants to make sure which float format Lisp should be reading,

a) specify the default format, as above

b) or use s, f, d, and l to specify the format (short float, single float, double-float, long-float). Example 1.0s0, 1.0f0, 1.0d0, 1.0l0




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: