Advertisement

conversion from double to int..

Started by June 22, 2001 10:27 AM
3 comments, last by Jesper T 23 years, 7 months ago
Just a little background : ive made a sinus[ 2048 ] array, and i''m gonna use it instead of the sin() function to gain speed ( forexample sinus[ 1024 ] = 1 instead of sin( pi/2 ) = 1 ) I use it in a function, when e.g. the value "value" is passed, it divides "value" by 3.1415.... and multiplies it with 2048 to get the right place in the array Before i can "return sinus[ value ];" i have to convert value to an integer value.. and i need to do it as fast as possible. Ive come up with this thing: int integer = 0; while( integer < value ) { integer++ } if( ( integer - value ) > 0.5 ) { integer -= 1; } ... but since the array is quite large this operation might take a lot of time ( especially since i might need to make it even larger ) does anyone know of a faster way to convert it/ find the closest integer ?
int integer;
integer = (int)(value + 0.5);
Advertisement
ok cool thanks
Why not store the integers in the array.
You then need the conversion only at initialization.
hmm no, cant do that, the values i use are angles and as the angles are in radians all of them (cept zero) have an infinte number of decimals.. well it might be possible to define a new angle-masurment system.. from -1024 to 2024 or sumthin hmm maybe its faster..

This topic is closed to new replies.

Advertisement