Subject: [clisp-list] short float coercion

From: Christophe Rhodes <[email-masked]>
Date: Wed Oct 22 12:49:09 2003

Hi,

The following demonstrates, if not an outright bug, at least a
suboptimality in float coercion.  I understand where it's coming from,
I guess, but it's quite undesireable to get 0.0 out at the end... is
there any chance of this being fixed to "do what I mean"?

  [1]> (lisp-implementation-version)
  "2.31 (released 2003-09-01) (built 3275820501) (memory 3275821131)"
  [2]> least-positive-short-float
  2.93874s-39
  [3]> (coerce * 'single-float)

  *** - floating point underflow
  1. Break [4]> 
  [5]> (ext:without-package-lock ("SYSTEM")
    (setf system::*inhibit-floating-point-underflow* t))
  T
  [6]> least-positive-short-float
  2.93874s-39
  [7]> (coerce * 'single-float)
  0.0

Cheers,

Christophe
-- 
http://www-jcsu.jesus.cam.ac.uk/~csr21/       +44 1223 510 299/+44 7729 383 757
(set-pprint-dispatch 'number (lambda (s o) (declare (special b)) (format s b)))
(defvar b "~&Just another Lisp hacker~%")    (pprint #36rJesusCollegeCambridge)

---

From: Sam Steingold <[email-masked]>
Date: Wed Oct 22 14:06:07 2003

> * Christophe Rhodes <[email-masked]> [2003-10-22 17:19:01 +0100]:
>

You are trying to coerce to SINGLE-FLOAT a number which is smaller than
LEAST-POSITIVE-SINGLE-FLOAT.  This means underflow, right?

>     (setf system::*inhibit-floating-point-underflow* t))

see <http://clisp.cons.org/impnotes/num-dict.html#no-underflow>

>   [7]> (coerce * 'single-float)

I am confused.
I have no idea what is going on.

(/ (float least-positive-short-float 1d0)
   (float least-positive-single-float 1d0))
==> 0.25

(/ (float most-positive-short-float 1d0)
   (float most-positive-single-float 1d0))
==> 0.5

(/ (float short-float-epsilon 1d0) (float single-float-epsilon 1d0))
==> 128

this means that even though short floats have 7 fewer bits of
precision, they manage to cover _more_ of the real line (twice as much)!

how could this be?

I hope Bruno will bring clarity here...

-- 
Sam Steingold (http://www.podval.org/~sds) running w2k
<http://www.camera.org> <http://www.iris.org.il> <http://www.memri.org/>
<http://www.mideasttruth.com/> <http://www.honestreporting.com>
Man has 2 states: hungry/angry and sate/sleepy.  Catch him in transition.

---

From: Raymond Toy <[email-masked]>
Date: Thu Oct 23 20:05:08 2003

>>>>> "Sam" == Sam Steingold <[email-masked]> writes:

    >> there any chance of this being fixed to "do what I mean"?

    Sam> You are trying to coerce to SINGLE-FLOAT a number which is smaller than
    Sam> LEAST-POSITIVE-SINGLE-FLOAT.  This means underflow, right?

Yes, I think so.

I notice that least-positive-single-float and
least-positive-normalized-single-float are the same value, and
corresponds to the normalized value on IEEE FP.  Is that intended?
You don't support unnormalized floats?

Ray

---

From: Bruno Haible <[email-masked]>
Date: Fri Oct 24 05:20:05 2003

> You don't support unnormalized floats?

Right. CLISP by design returns the same floating point results on all
platforms (to reduce portability problems). Therefore it has a floating-point
emulation built in for platforms that don't support IEEE FP. And I
didn't spend time on unnormalized floats and various types of NaNs that
are not generally useful:
  - When you got a NaN in your program, your program is broken. As a
    developer, one then ususally spends time determining where the
    NaN came from. It's better to signal an error in this case.
  - When you got unnormalized floats in your program, your results will
    have a greatly reduced accuracy anyway. Since clisp has the means
    to cope with this - LONG-FLOATs of variable precision - it doesn't
    need to support unnormalized floats.

Bruno

---

From: Bruno Haible <[email-masked]>
Date: Fri Oct 24 06:17:03 2003

> precision, they manage to cover _more_ of the real line (twice as much)!

The SINGLE-FLOAT in-memory format in clisp follows IEEE 754, which reserves
one particular exponent value for Infinitys and NaNs. Since clisp doesn't
support these beasts, neither in SINGLE-FLOAT nor in SHORT-FLOAT, it
means that one of the 255 possible exponents in the SINGLE-FLOAT format
is wasted. The SHORT-FLOAT format is specific to clisp, not adhering to
any standard, and therefore doesn't need to waste one of the 255 possible
exponents.

Bruno

---

From: Christophe Rhodes <[email-masked]>
Date: Fri Oct 24 06:36:04 2003

> LEAST-POSITIVE-SINGLE-FLOAT.  This means underflow, right?

I'm so sorry -- I completely missed this.  You're quite right, in pure
Common Lisp terms.

I was expecting that least-positive-single-float would be the IEEE
value for the least positive single float, not just the least positive
normalized single float, but there's no reason that that should be the
case.  (I don't know why you'd do it this way, but I'm perfectly
willing to believe that there are legitimate reasons for doing so :-).

Cheers,

Christophe