help-octave
[Top][All Lists]

1 does not equal 1

 From: Ted Harding Subject: 1 does not equal 1 Date: Sun, 10 Jul 1994 18:25:34 +0100 (BST)

```" To: address@hidden
" Subject: float->double conversion problem?
"
" Perhaps someone can help me here: it seems that one is not equal to one...
"
" Octave, version 1.0.  Copyright (C) 1992, 1993, 1994 John W. Eaton.
" This is free software with ABSOLUTELY NO WARRANTY.
" For details, type `warranty'.
"
" octave:1> format long e
" octave:2> 1
" ans =  1.00000047683363e+00
" octave:3> 1/1
" ans =  1.00000047683363e+00
" octave:4> quit
"
" So, what exactly does this mean? All attempts at setting a variable to exactly
" 1.0 have failed... is this a bug in the printing subroutines, or is a
" conversion between a float and a double going on somewhere that twiddles with
" the low order bits on the floating point number?
"
----------------------------------------------------------------------------
Compare the above with the following (copied from the output):
=====================================
Octave, version 1.0.  Copyright (C) 1992, 1993, 1994 John W. Eaton.
This is free software with ABSOLUTELY NO WARRANTY.
For details, type `warranty'.

octave:1> format long e
octave:2> 1
ans =  1.00000000000000e+00
octave:3> 1/1
ans =  1.00000000000000e+00
octave:4> 1.0
ans =  1.00000000000000e+00
octave:5> quit
=====================================
This was produced by a pre-compiled octave binary (as found in
ftp.che.utexas.edu:/pub/octave/BINARIES). If yours was compiled by you,
there may be a compiler problem. What you observe is also produced by
a well-known trap in C with printf and scanf. If 'x' is a double
then scanf("%f",&x) stores the input as a _float_ in the top half of 'x'
and does not touch the bottom half, which may contain garbage. If you then
do printf("%f",x) you get the garbage as well. Printf is supposed to
automatically cast from float to double before formatting for printing,
so the trouble should not occur in printf. The cure for the C trap is
to make sure that you use scanf("%Lf",&x). Something analogous may be