Csound Csound-dev Csound-tekno Search About

Floating point problems and sscanf

Date1998-04-07 11:42
FromStephen Brandon - SysAdmin
SubjectFloating point problems and sscanf
Hi,

I am working on the NeXTstep version of 3.47, and intend to submit  
the changes to the maintainers for inclusion in 3.48.

I am working on a NeXTstep-running P133, and have found some very  
annoying floating point errors. I wonder if anyone else, on any  
other platform, has successfully tackled this sort of problem.

I have also tried out experiments with IRIX 5.2, 6.2, and also with  
a more recent version of gcc on NS3.3.

ALL of the platforms above suffer the same problem!

I discovered the problem when trying to compile a score and  
orchestra with the following kr, sr, ksmps, resulting in the  
following error:

sr = 22050.000000, kr = 1000.000000, ksmps = 22.049999
error:  inconsistent sr, kr, ksmps

Of course, I had tried to use ksmps as 22.05.

At first I thought it was the sscanf in otran.c (constndx) that was  
playing up, but then I experimented with some code fragments only  
to find that even if I assigned "(float) newval = 22.05", "if  
(newval == 22.05)" would fail.

Now I realise that floating point data is not perfect, but this is  
ridiculous.

Is this something that can be fixed/worked around, or do we all  
just grit our teeth and try to avoid certain numbers?

Cheers,
Stephen Brandon
---
Systems Administrator,
Department of Music,		e-mail: S.Brandon@music.gla.ac.uk
14 University Gardens,			(NeXT mail welcomed)
University of Glasgow,		Tel: 	+44 (0)141 330 6065
Glasgow.			Fax:	+44 (0)141 330 3518
Scotland
G12 8QH

Date1998-04-07 17:13
FromEli Brandt
SubjectRe: Floating point problems and sscanf
Stephen Brandon - SysAdmin wrote:
> sr = 22050.000000, kr = 1000.000000, ksmps = 22.049999
> error:  inconsistent sr, kr, ksmps
> 
> Of course, I had tried to use ksmps as 22.05.
> 
> At first I thought it was the sscanf in otran.c (constndx) that was  
> playing up, but then I experimented with some code fragments only  
> to find that even if I assigned "(float) newval = 22.05", "if  
> (newval == 22.05)" would fail.

As you probably know, 22.05 can't be represented exactly.  I'm not
certain what's going on in your code fragment (I blindly avoid all
equality tests on floating-point, myself), but I imagine newval is
being promoted to a double and compared against a longer approximation
of 22.05.

The simplest approach is to avoid assigning to all three of these
values.  Presuming you need the third, you could just call it
something else that Csound won't try to check for consistency.
(Might "ksmps = sr/kr" work better?  Dunno whether it's even legal.)

When Csound is given two of the three, it computes the third internally -- 
might want to make it available to the .orc.  (If it already does this,
well, good.)

-- 
     Eli Brandt  |  eli+@cs.cmu.edu  |  http://www.cs.cmu.edu/~eli/