Csound Csound-dev Csound-tekno Search About

[Csnd] Bug in printks

Date2012-07-01 07:12
FromJim Aikin
Subject[Csnd] Bug in printks
While trying to track down a bug in my own code (found it eventually), I had
recourse to printks. For a while, I was flummoxed by the fact that an
important variable was taking on an obviously impossible value. This
variable could ONLY be set in my code to 0 or -2, but printks was reporting
it as -1.

After tearing my hair out for about twenty minutes because I thought that
must be the source of my bug, I discovered that printks was simply giving me
erroneous information. The actual value of my variable was indeed -2.

Here's a test .csd that shows the problem:





sr = 44100
ksmps = 1
nchnls = 1

instr 1
kval init -2
knorepeat init 0
if (knorepeat == 0) then
	printks "kval is %d with d.\n", 0, kval
	printks "kval is %f with f.\n", 0, kval
endif
knorepeat = 1
endin



i1 0 0.1



And here's the output:

new alloc for instr 1:
kval is -1 with d.
kval is -2.000000 with f.

I would guess this may have something to do with floating-point math,
minuscule rounding errors, and the fact that I happened to be using a
negative number. I frankly don't think that matters. printks ought to give a
humanly sensible output, even with a negative number. (With a positive
number, both print statements report 2.)

--Jim Aikin





--
View this message in context: http://csound.1045644.n5.nabble.com/Bug-in-printks-tp5713994.html
Sent from the Csound - General mailing list archive at Nabble.com.

Date2012-07-01 13:58
FromLouis Cohen
SubjectRe: [Csnd] Bug in printks
Jim,

I played around with your example and concluded that the problem may  
be some rounding error with integer formatting (%d).

Here's my example, followed by the output:






sr = 44100
ksmps = 1
nchnls = 1
instr 1
kval init -5.0001
knorepeat init 0
if (knorepeat == 0) then
printks "kval is %d with d. ", 0, kval
printks "kval is %5.2f with f. ", 0, kval
endif
knorepeat = 1
endin


i1 0 0.1



Csound version 5.14 (float samples) Oct 10 2011
0dBFS level = 32768.0
orch now loaded
audio buffered in 256 sample-frame blocks
PortAudio V19-devel (built Feb 12 2010 09:42:54)
PortAudio: available output devices:
0: Built-in Output
1: Built-in Line Output
2: Built-in Digital Output
3: Audio Kontrol 1
PortAudio: selected output device 'Audio Kontrol 1'
writing 512-byte blks of shorts to dac3
SECTION 1:
new alloc for instr 1:
kval is -4 with d. kval is -5.00 with f. Score finished in  
csoundPerformKsmps().
inactive allocs returned to freespace
end of score.	 overall amps: 0.0
overall samples out of range: 0
0 errors in performance
18 512-byte soundblks of shorts written to dac3


On Jul 1, 2012, at 2:12 AM, Jim Aikin wrote:

> While trying to track down a bug in my own code (found it  
> eventually), I had
> recourse to printks. For a while, I was flummoxed by the fact that an
> important variable was taking on an obviously impossible value. This
> variable could ONLY be set in my code to 0 or -2, but printks was  
> reporting
> it as -1.
>
> After tearing my hair out for about twenty minutes because I thought  
> that
> must be the source of my bug, I discovered that printks was simply  
> giving me
> erroneous information. The actual value of my variable was indeed -2.
>
> Here's a test .csd that shows the problem:
>
> 
> 
> 
> 
> sr = 44100
> ksmps = 1
> nchnls = 1
>
> instr 1
> kval init -2
> knorepeat init 0
> if (knorepeat == 0) then
> 	printks "kval is %d with d.\n", 0, kval
> 	printks "kval is %f with f.\n", 0, kval
> endif
> knorepeat = 1
> endin
>
> 
> 
> i1 0 0.1
> 
> 
>
> And here's the output:
>
> new alloc for instr 1:
> kval is -1 with d.
> kval is -2.000000 with f.
>
> I would guess this may have something to do with floating-point math,
> minuscule rounding errors, and the fact that I happened to be using a
> negative number. I frankly don't think that matters. printks ought  
> to give a
> humanly sensible output, even with a negative number. (With a positive
> number, both print statements report 2.)
>
> --Jim Aikin
>
>
>
>
>
> --
> View this message in context: http://csound.1045644.n5.nabble.com/Bug-in-printks-tp5713994.html
> Sent from the Csound - General mailing list archive at Nabble.com.
>
>
> Send bugs reports to the Sourceforge bug tracker
>            https://sourceforge.net/tracker/?group_id=81968&atid=564599
> Discussions of bugs and features can be posted here
> To unsubscribe, send email sympa@lists.bath.ac.uk with body  
> "unsubscribe csound"
>


Date2012-07-01 16:35
FromJim Aikin
Subject[Csnd] Re: Bug in printks
That's a good test, Louis: Even with a value of -5.0001, it's rounding down
to -4. I'm not a real programmer, but I would hazard a guess that what this
means is that the floating-point number format is not the culprit. It may be
something simpler than that -- just an off-by-one error.

--JA

--
View this message in context: http://csound.1045644.n5.nabble.com/Bug-in-printks-tp5713994p5713999.html
Sent from the Csound - General mailing list archive at Nabble.com.

Date2012-07-01 17:38
Fromjpff@cs.bath.ac.uk
SubjectRe: [Csnd] Re: Bug in printks
Thinking out loud....

The value printed it (int)MYFLT2LRND(xx) where xx is the value.
MYFLT2LRND is usually defined as lrint (for doubles) or lrintf (for floats).
The immediate worry is that this macro is used all over the place. 
Defined as "round argument to the nearest integer value, using the current
rounding direction".  I would have thought -5 is closest to -5.0 in any
rounding direction!

..... but there is unreconstructed code.  Fixed in my sources and yes the
rounding ignored the case of nedative numbers.  A variant of this was
fixed a long time ago but there was a different case

==John ff

> That's a good test, Louis: Even with a value of -5.0001, it's rounding
> down
> to -4. I'm not a real programmer, but I would hazard a guess that what
> this
> means is that the floating-point number format is not the culprit. It may
> be
> something simpler than that -- just an off-by-one error.
>
> --JA
>
> --
> View this message in context:
> http://csound.1045644.n5.nabble.com/Bug-in-printks-tp5713994p5713999.html
> Sent from the Csound - General mailing list archive at Nabble.com.
>
>
> Send bugs reports to the Sourceforge bug tracker
>             https://sourceforge.net/tracker/?group_id=81968&atid=564599
> Discussions of bugs and features can be posted here
> To unsubscribe, send email sympa@lists.bath.ac.uk with body "unsubscribe
> csound"
>
>
>
>