PDA

View Full Version : [solved] convert floating point to integer



noeska
01-09-2009, 05:42 PM
System: XP pro on virtualbox
Compiler\IDE: fpc 2.2.4
API: OpenGLes (mbx lite)

I am trying to convert floating point values to integer. I need that because i need to support hardware that does not support floating point values.
For now i use the following function:

function f2vt(value: single): integer;
var
temp: integer;
begin
temp := integer( single(value) * single(65535));
writeln(integer(temp));
result := integer(temp);
end;

But the results are all wrong for negative numbers.
if i feed -0.4 i get: -2147457434 (i expected -26214)
if i feed 0.4 i get: 26214 (correct)

Legolas
01-09-2009, 06:25 PM
function f2vt(value: single): integer;
var
temp: integer;
begin
temp := integer(trunc(single(value) * single(65535)));
writeln(integer(temp));
result := integer(temp);
end;


works here, though I don't know if it is a suitable solution for you

noeska
01-09-2009, 06:43 PM
Thank you. That seems to work for me too.

Legolas
01-09-2009, 07:22 PM
You are welcome :)
I had similar problems converting libnds for fpc. It was a real nightmare, because I had to check all those functions for type conversions, value by value, one by one, comparing the results from c and pascal code, and testing it on an emulator and on real hardware :vomit::