Plug-ins, Hosts, Apps,
Hardware, Soundware
Developers
(Brands)
Videos Groups
Whats's in?
Banks & Patches
Download & Upload
Music Search
KVR
   
KVR Forum » DSP and Plug-in Development
Thread Read
dangerous floating point math
Goto page 1, 2  Next
xoxos
Mr Entertainment
- profile
- pm
- www
PostPosted: Mon Feb 18, 2013 10:56 am reply with quote
seeking insight here....


i've had a lot of problems with something i didn't expect to. my envelope code has a 32 bit float "level" variable that ranges from 0.0f to 1.0f

the release is performed, imo predictably by decrementing. level -= dec. a check to shut the envelope off is then performed.. this used to be if (level <= 0.f) envelope = off, but i've had significant issues with the code not reaching this point. if i increase the threshold to (level < .0005f) the performance is much better, notes rarely hang. but, this is of course lousy resolution.

okay, so f**k me. i haven't made any great effort to educate myself on the thresholds of floating point, i know that after a certain size, precision is lost (eg. integers over a certain size cannot be precisely indicated). the solution is to scale my envelope to 100000 or something instead of 1.

i'm guessing that 32 bit floating point precision is resulting in something like small#a minus small#b = no discernible difference, therefore floating point calculation returns the same, there is effectively no subtraction. that seems reasonable to me, but i wouldn't expect to reach this threshold until about six decimal places.

anyone?
----
neither a follower nor a leader be
http://www.xoxos.net - free vst
^ Joined: 29 Apr 2002  Member: #2639  Location: i might peeramid
lorcan
KVRist
- profile
- pm
- www
PostPosted: Mon Feb 18, 2013 12:14 pm reply with quote
You might want to have a look at this http://en.cppreference.com/w/cpp/types/numeric_limits/epsilo n

On my machine std::numeric_limits<f32>::epsilon() is 1.19209290e-007 (the spec says it should be smaller than 1e-5).
Maybe the floating point precision compiler settings have an effect on this too, I haven't checked (see for example http://msdn.microsoft.com/library/e7s85ffb.aspx)
^ Joined: 25 Sep 2001  Member: #1160  Location: Paris, France
AdmiralQuality
KVRAF
- profile
- pm
- e-mail
- www
PostPosted: Mon Feb 18, 2013 6:21 pm reply with quote
xoxos wrote:
the solution is to scale my envelope to 100000 or something instead of 1.


No.

What range is dec? What's the lowest value it ever has? How do you calculate it?
^ Joined: 10 Oct 2005  Member: #83902  Location: Toronto, Canada
camsr
KVRAF
- profile
- pm
PostPosted: Mon Feb 18, 2013 7:21 pm reply with quote
Are you scaling the dec value per increment? If you are using multiplication by a value smaller than one anywhere with dec than you are slowing and possibly sustaining the envelope. It looks linear so far. What kind of dynamic range are you expecting? Single or double precision?
----
^ Joined: 16 Feb 2005  Member: #58183  
camsr
KVRAF
- profile
- pm
PostPosted: Mon Feb 18, 2013 7:33 pm reply with quote
http://www.h-schmidt.net/FloatConverter/IEEE754.html

You can figure a threshold with defined dynamic range by multiplying every power change of the exponent segment by 6 db. 2^-24 exponent would be -144 dBFS. Then just use the hex value as a constant instead of the numeric.
----
^ Joined: 16 Feb 2005  Member: #58183  
FabienTDR
KVRist
- profile
- pm
PostPosted: Tue Feb 19, 2013 7:46 am reply with quote
I suspect there is a bug in your control logic which is not related to precision issues. btw, the higher the scale, the bigger the epsilon size. Scaling the range up will not help.

However, you can reduce precision problem to irrelevant levels by using double precision floats, careful dithering and/or by working in the dB domain (btw, the latter is practically a "must" for accurate followers in the audio context).
----
Fabien from Tokyo Dawn Records

Check out my audio processors over at the Tokyo Dawn Labs!
^ Joined: 23 Feb 2012  Member: #275694  
xoxos
Mr Entertainment
- profile
- pm
- www
PostPosted: Tue Feb 19, 2013 10:02 am reply with quote
thanks for the references.. i've seen the information, hadn't internalised it as it hasn't previously been a necessary part of discerning whether an algorithm works..

increment value is 1.f / (env time in samples) so at 32 bits it's bound to err.

gotta go fix something to lessen the amount of yelling at me before further consideration is possible..
----
neither a follower nor a leader be
http://www.xoxos.net - free vst
^ Joined: 29 Apr 2002  Member: #2639  Location: i might peeramid
lorcan
KVRist
- profile
- pm
- www
PostPosted: Tue Feb 19, 2013 10:19 am reply with quote
xoxos wrote:
increment value is 1.f / (env time in samples) so at 32 bits it's bound to err.


I believe 64bit doubles are mandatory here too, as summation errors would go over the roof in simple precision anyway even if you found a way to make that substraction accurate at low values.

More interesting reading (for me & others too I suppose Cool )
http://en.wikipedia.org/wiki/Kahan_summation_algorithm
http://www.drdobbs.com/floating-point-summation/184403224
^ Joined: 25 Sep 2001  Member: #1160  Location: Paris, France
AdmiralQuality
KVRAF
- profile
- pm
- e-mail
- www
PostPosted: Tue Feb 19, 2013 11:16 am reply with quote
xoxos wrote:

increment value is 1.f / (env time in samples) so at 32 bits it's bound to err.


That should be fine actually. You have 24 bits of mantissa in a 32 bit float. That's 174 seconds at 96 kHz.

But I was asking for the ACTUAL value of dec. Run it in the debugger and see what it really is. When in doubt, get empirical.
^ Joined: 10 Oct 2005  Member: #83902  Location: Toronto, Canada
lorcan
KVRist
- profile
- pm
- www
PostPosted: Tue Feb 19, 2013 11:36 am reply with quote
AdmiralQuality wrote:
xoxos wrote:

increment value is 1.f / (env time in samples) so at 32 bits it's bound to err.


That should be fine actually. You have 24 bits of mantissa in a 32 bit float. That's 174 seconds at 96 kHz.

But I was asking for the ACTUAL value of dec. Run it in the debugger and see what it really is. When in doubt, get empirical.


Except it's not so simple as
(thresh * 96000smp) <= 2^24 ~= 16.7M (approx 1/std::numeric_limits<float>::epsilon())

That would assume rounding errors don't propagate at each step. You'd have to actually store ints for that to work, or use special summation formulae.

I just did a quick test:


float fSum = 0.f;
float fVal = 1e-6f;

enum { nSum = 1000000 };

for (uInt i = 0; i < nSum; ++i)
{
  fSum += 1e-6f;
}



at then end fSum equals 1.00904, so a delta of 0.009039 -> roughly 1% error
Last edited by lorcan on Tue Feb 19, 2013 11:44 am; edited 3 times in total
^ Joined: 25 Sep 2001  Member: #1160  Location: Paris, France
AdmiralQuality
KVRAF
- profile
- pm
- e-mail
- www
PostPosted: Tue Feb 19, 2013 11:42 am reply with quote
lorcan wrote:
AdmiralQuality wrote:
xoxos wrote:

increment value is 1.f / (env time in samples) so at 32 bits it's bound to err.


That should be fine actually. You have 24 bits of mantissa in a 32 bit float. That's 174 seconds at 96 kHz.

But I was asking for the ACTUAL value of dec. Run it in the debugger and see what it really is. When in doubt, get empirical.


Except it's not so simple as
(thresh * 96000smp) <= 2^24 ~= 1.67M (approx 1/std::numeric_limits<float>::epsilon())

That would assume rounding errors don't propagate at each step. You'd have to actually store ints for that to work, or use special summation formulae.


2^24 is 16 million.

Anyway, I'd have to sit down and actually think to figure this out as the exponent will come into it as well (hence my advice to check it empirically) but 32 floats should deal with this kind of range just fine.

You could always calculate it it the other way, by counting samples, if you don't mind a multiplication at every sample. But I'd take a look at what the actual values are first, I suspect the issue isn't what xoxos thinks it is.
^ Joined: 10 Oct 2005  Member: #83902  Location: Toronto, Canada
lorcan
KVRist
- profile
- pm
- www
PostPosted: Tue Feb 19, 2013 11:44 am reply with quote
AdmiralQuality wrote:


2^24 is 16 million.

Anyway, I'd have to sit down and actually think to figure this out as the exponent will come into it as well (hence my advice to check it empirically) but 32 floats should deal with this kind of range just fine.

You could always calculate it it the other way, by counting samples, if you don't mind a multiplication at every sample. But I'd take a look at what the actual values are first, I suspect the issue isn't what xoxos thinks it is.


Yep, sorry I was a bit too quick there, the rest of the calculation holds tough.
I just edited my prev post with a test.
Cheers
^ Joined: 25 Sep 2001  Member: #1160  Location: Paris, France
AdmiralQuality
KVRAF
- profile
- pm
- e-mail
- www
PostPosted: Tue Feb 19, 2013 11:46 am reply with quote
Small error is irrelevant to what he's trying to achieve. As long as it continues to decrement it has to pass zero and go negative at some point. He feels it's never happening though.

Again, what's dec, EXACTLY?
^ Joined: 10 Oct 2005  Member: #83902  Location: Toronto, Canada
camsr
KVRAF
- profile
- pm
PostPosted: Tue Feb 19, 2013 2:45 pm reply with quote
lorcan wrote:
xoxos wrote:
increment value is 1.f / (env time in samples) so at 32 bits it's bound to err.


I believe 64bit doubles are mandatory here too, as summation errors would go over the roof in simple precision anyway even if you found a way to make that substraction accurate at low values.

More interesting reading (for me & others too I suppose Cool )
http://en.wikipedia.org/wiki/Kahan_summation_algorithm
http://www.drdobbs.com/floating-point-summation/184403224


Truncating the result should provide the most useful behavior for an envelope. I suppose the error is not as severe as the result in this case.
----

Last edited by camsr on Tue Feb 19, 2013 2:52 pm; edited 1 time in total
^ Joined: 16 Feb 2005  Member: #58183  
AdmiralQuality
KVRAF
- profile
- pm
- e-mail
- www
PostPosted: Tue Feb 19, 2013 2:50 pm reply with quote
I did it the same way and it worked for me. (I've since changed everything to doubles but never had any problem when it was all 32 bit.)

You're thinking too much. You can definitely count to 16 million with floats. The problem is something ELSE. Probably dec is zero or something.

Wait, is this not just the envelope counter but also the output level? If so, what happens if a long release time activates when a decay or sustain level was almost at zero, does it start counting down from THERE? Or from 1? (Another way of asking, are they rate envelope segments or time envelope segments?)
^ Joined: 10 Oct 2005  Member: #83902  Location: Toronto, Canada
All times are GMT - 8 Hours

Printable version
Page 1 of 2
Goto page 1, 2  Next
Display posts from previous:   
ReplyNew TopicPrevious TopicNext Topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Username: Password:  
KVR Developer Challenge 2012