Forums > Javascript

[JS] float precission , number division

Aug 09 2013 | 10:17 am

Hello .

Im feeling stupid . Please look at the code and explain to my wonky head why i cant get the same division result of every cycle ?

function msg_int(n){
	var div = 1/4 ;
	var result = (n*div)/1.5

	/* filter test output  
	every "1" of 6 steps 
	triggers result of a division 
	to the max window */

	var m = n % 6 ;
	if(m === 1){

for example :
when i send " 1 " through the inlet , what im getting in 64bit bit precision is :

but when " 7 " is sent :

why is that happening ? what is going on with the decimals ?

edit :


Aug 09 2013 | 11:34 am

Floating-point is only finite accuracy. If you want infinite accuracy, you need an infinite number of bits.

I’ve written about this hundreds of times on the list. Find and read Peter Elsea’s tutorial on floating point, or get the full dope on IEEE 754. Wikipedia is, as usual, not a bad place to start

You may also want to look here or here.

Aug 09 2013 | 11:40 am

Faster than you can say, "Cue Peter." :)

Aug 09 2013 | 11:58 am

yes i see , thank you . Do you know where can i find Peter’s tutorial on floats ? i found some pdf link but its gone actually

Aug 10 2013 | 2:54 am

Try Max&Numbers.pdf and go to the "The Lowdown on Floats" section. It’s pretty short and covers most of the gotchas you’ll come across.

Hope this helps.

Aug 10 2013 | 11:58 am

Yes thank you ,its very informative . unfortunately it doesnt help me too much . Understanding of the problematics not always guarantees of solving the problem (i might be not capable), not sure for what i should aim for now :/ really . Now i just seeing how floats work ,but no way to get around it . Or there is hidden message "you need to live with it " ? . Its not clear . im still stupid

Aug 10 2013 | 1:28 pm

Do I dare ask? What problem are you trying to solve for which 15 decimal digits of precision is not sufficient?

Aug 11 2013 | 2:19 am

0.1666……….. represents triplet bar division . its used to calculate position ,distances ,durations of notes , its used to compute other crucial measurements , this way im always in offset . it would not be a problem if this would be used for representation , the problem is that i need to do operations on distances against positions etc . when adding ,multiplying i will not be able to "place" notes in a appropriate "time" unit :/

Aug 11 2013 | 7:14 am

Sorry, I don’t understand your last post. Perhaps if you post a short example that shows why 64-bit precision is inadequate for a specific task, someone will be able to suggest a solution to you.

Aug 11 2013 | 11:45 am

For working with musical time units (bar divisions) you could use rational numbers and rational arithmetic as provided by the "bach" externals.

Aug 12 2013 | 11:42 am

If your smallest note value is, say, a triplet eighth note, then count triplet eighth notes:

0 – first beat (quarter) of bar
1 – second triplet eighth
2 – third triplet eighth
3 – second quarter

If you’re mixing triplets with straight eighths, then count the largest common subdivision, in this case, triplet sixteenths (2 of those equal a triplet eigth; 3 make a straight eighth).

And so on. Most MIDI sequencers used some fairly small subdivision of the quarter note and hope that the granularity is small enough to support every rhythmic unit that’s likely to be used. If you know what rhythmic units you want to use, then just pick something sufficiently small to cover all of them in integer combinations.

You can use floats if you really feel obliged to and you’ll never hear the difference. At a moderately slow tempo, say quarter = 40, the inaccuracy you’re worrying about is in the area of one millionth of a nanosecond. Yes, of course errors can add up. But with these numbers, your patch could have been running since the Big Bang and you’d still be accurate to within a single sample.

So, unless I’ve misunderstood you, what are you worrying about exactly?

(Caveat: back-of-the-envelope calculations here, I might be off by a decimal place or two. Still.)

Aug 12 2013 | 12:33 pm

Thank you guys for your effort and patience !

yes , thats very convenient and smart convention .
That scenario im struggling with is based on the same sort of bar division as Ableton guys invented/used . If i want 16th triplets i need to divide bar on 24 steps which is very natural for me . and this is why i ended up with floats as time index for note representation in bar range . I did it because im working with API where im getting notes from ableton clips (the same time index) ,so this way i dont need to make any TIME conversion . Im working on that notes within my JSUI clip representation and those changes are reflected back to ableton clip .
I wouldnot affraid if i have tiny offset at notees . For a part of manipulation maybe i can compare small differences to SNAP in a right position (but i cant be sure what is a real division result actually ) .So i think that when it comes to note selection ,duplication or other note operations then there might be some surprises . Maybe i should not care too much there but if i offset my BAR RANGE selection to a triplet time then perhaps i will end up with shorter or longer bar length when i back to ableton clip (in result my clip may get off-synced after some time ). Beside other struggles this might be important one .
Even if i will find the other way for math within my code ,at some point i will be forced to convert my time index to ableton time index ,which is represented in floats :/ so i will need to divide 1 anyway :/

SO dividing quarters may help me still there ?

  1. seq


Viewing 12 posts - 1 through 12 (of 12 total)

Forums > Javascript