Float Points

Blitz3D Forums/Blitz3D Beginners Area/Float Points

Aussie(Posted 2012) [#1]
Hello all,
It's been a fair while since i have done any coding or posted here.
I am having some problems with float points with a sound program i am playing with. I need volume & panning to be 0.00, -1.00 & 1.00.
When i adjust the levels & when i get close to the 0.00, 1.00 or -1.00 i end up with numbers like .099999999 or 7.1234e-009.
What can be done to keep the float points in the ranges that i need?

Up & down arrows to adjust the volume.
Left & right arrows to adjust the panning.


Last edited 2012


Rob the Great(Posted 2012) [#2]
This might interest you:

http://blitzbasic.com/Community/posts.php?topic=95814#1105876

Basically, for speed reasons, Blitz will represent values close to 0.1 and 0.0, but the values aren't exact. The reason being is that 0.1 happens to be infinitely recurrent (See Yasha's post in my link) when expressed in binary fractions. In Blitz alone, floats are unable to represent these values perfectly. You would need something like a Big Number in C based languages to represent those values perfectly, but then you compromise speed, which might be a problem in audio manipulation programs.

Now, what's interesting is that you can manually set the floats with something like:
variable# = 0.1
Text 0,0,variable#

In my experience, Blitz seems to be able to retain these values just fine, but it can't get there by itself through math operations (e.g. 0.2 - 0.1). I'm not sure why that is.

My question: is there a reason why you need those values to be perfect? If so, then I'm sorry to say that it's above my knowledge on how to do that. But, in most cases, you can use .099999999 or 7.1234e-009 (0.0000000071234), and you will never know the difference. You just have to watch out when you program to make sure you don't use = with floating point numbers. Instead, use:
If variable# <= value# + 0.1 And variable# >= value# -0.1

to allow for a greater error margin.

Hopefully, that answers some questions. This is about all I know on the subject.

Last edited 2012


Aussie(Posted 2012) [#3]
Thanks for the reply Rob.

It's not of great importance, I mainly want it so that when the levels are displayed on the screen i can have them at 0.00, 1.00 or -1.00.

What i am trying to do is make something like this.
http://naturesoundsfor.me/

I found it the other day & it's great to listen to as i check out things on the net. The only thing is, i want something like this offline & with more options.

I have been doing a fair bit of percussion with a range of different instruments & would like to be able to create soundscapes with the program & record them. Then be able to record the percussion & mix the soundscape with the percussion recordings to create meditative music.
I will use other software for mixing but i just want to create something for generating soundscapes.


Yasha(Posted 2012) [#4]
I mainly want it so that when the levels are displayed on the screen i can have them at 0.00, 1.00 or -1.00


Well, if you want a consistent three digit width for the decimal representation, you're probably going to have to clean it up manually anyway...

...so the easiest solution might well be to simply represent the value internally as an integer between -100 and 100, and have your own toString routine that inserts the fractional point in the right place? This is probably much simpler than trying to get floats to play nice, because that'd involve a lot of checking of values.


---


As a programming-in-general aside, you may find it helpful not to think of the distinction as being "whole numbers" vs. "fractions", but rather "fixed-point" vs. "floating-point". If the decimal point doesn't move (or more properly, the number of values required is less than the range of ints), any addition and subtraction operations are always equivalent to integer addition and subtractions where the integers represent smaller units (multiplication and division is a little more complicated, but not much). You can do everything with ints, and just convert the size when the time comes to represent the value.

Floating point values are not specifically useful for fractions, so much as to represent numbers whose range is actually greater than the number of values that can be accurately represented by the size of the data type you're manipulating, while still taking advantage of relatively fast math operations (bignums, even using highly optimised libraries, are slow). Unless the point is actually going to float, you should always consider whether you can easily achieve your goals with a fixed-point number instead.

</ramble>

Last edited 2012


Aussie(Posted 2012) [#5]
Thanks Yasha.

Kind of confused :)

So if i want the number that is to be displayed, lets say now from -100 to 100, would i then just divide say for example -2 by 100 & pass the resulting -0.02 into the ChannelPan & blitz would set it to the closest it could to that number while still having a nice whole number being displayed?


Yasha(Posted 2012) [#6]
In your specific case I actually meant not doing a division for display - instead pass -2 to a function that converts it to the string "-0.02", by either inserting a dot where appropriate (i.e. before the last two digits), or padding with zeroes first, and then inserting a dot.

For passing an actual volume to the channel functions, I would then just do a "n / 100.0" (note the .0) on the stored int (not the display string!) as you pass it to the function, and there's no need to ever store the float version of the value.

Depends on your other needs for transforming the number, of course.

Last edited 2012


Aussie(Posted 2012) [#7]
Got it working.
Thanks for the help, here is the code.