Strange values using float or double for array

Posted By: Newbie999

Strange values using float or double for array - 05/28/08 03:32

Another newbie here,

When I declare an array as "var" I can display its contents properly. However, when I declare it as "float" or "double", I get really strange values displayed. I thought using float or double would simply store better precision. Is there more to it than that?

// sample code below displays a strange value of -1056768 instead of -1.


float direction[3] = {0,-1,0};


PANEL* pDisplay =
{
digits (10, 60, "direction[1] = %0.f", *, 1, direction[1]);


flags = VISIBLE;
}
Posted By: JibbSmart

Re: Strange values using float or double for array - 05/28/08 05:13

panels can't deal with doubles or floats; they need vars. you probably could typecast it:
Code:
digits (10, 60, "direction[1] = %0.f", *, 1, (var)direction[1]);


but that's just a guess.

julz
Posted By: Newbie999

Re: Strange values using float or double for array - 05/30/08 01:12

Thanks for explaining about digits. However, there is still something mysterious going on even when I am not displaying the digits.

Running the following code will produce a red dot slightly above the center of the screen. By simply redefining the "myvector" variable from var to double, the dot will be displayed in a different location. By changing it to float, the dot is moved completely off the screen. Why would the location of the dot move by simply changing the precision of the variable??

////////

#include <acknex.h>
#include <default.c>

var myvector[3] = {0,-1,50};

function main()
{
level_load("small.hmp");
while(1)
{
draw_point3d(myvector,vector(0,0,255),100,5);
wait(1);
}

}
Posted By: zazang

Re: Strange values using float or double for array - 05/30/08 02:04

maybe u need to typecast myvector to var here too..
Posted By: Excessus

Re: Strange values using float or double for array - 05/30/08 14:40

It's the same problem as your first one. When a function expects a var, you must pass it a var. When a function expects a VECTOR* (which is the same as a var[3]), you must pass a VECTOR*.

What happens when you don't is that the bits are interpreted in a way they wheren't intended to when they where written. For example, a var contains bits for the integral parts and bits for the fractional part. A float or double contain bits for the base and exponent.
Posted By: Newbie999

Re: Strange values using float or double for array - 06/01/08 14:07

Thanks for explaining all that. It makes sense.

So if VECTOR* is the same as var[3], then I guess I will not be able to get better than 3 decimal place accuracy while doing vector rotations. Or is there another way to get better precision doing vector rotations?
Posted By: Joey

Re: Strange values using float or double for array - 06/01/08 14:29

you could implement your own vector functions.
© 2024 lite-C Forums