Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » I'm getting crazy with the fixed type!!!

This thread is locked; no one can reply to it. rss feed Print
I'm getting crazy with the fixed type!!!
Johannes Bergmark
Member #4,867
July 2004
avatar

Maybe I did wake at the wrong side today.

But this code doesn't work so well right now:

double fPI = 3.141592653589793;
fix fDeltaV = ftofix(fPI);

textprintf_ex(sBuffer, font, 10, 10, makecol(255, 100, 200),-1, "fDeltaV=%f", fixtof(fDeltaV));

The problem is that the output is 0.141586.

Is my computer condemned or not? Or is it my brain?

"There are no Mines for the Sword." --?

"Det finns inga Minor för Svärdet." --?

Jonatan Hedborg
Member #4,886
July 2004
avatar

EDIT: never mind.
Odd. But why are you using fixed point math? Is it really necessary?

-------
Sweden: Free from the shackles of Democracy since 2008-06-18!

Matt Smith
Member #783
November 2000

I always use int for storing fixes. Try int fDeltaV = ftofix(fPI);
0.141586 looks like just the bottom 16 bits to me.

Second possibility (pure guess time), try defining fPI as float rather than double. ftofix() is a macro on some platforms and may be doing funky bitshifting stuff with an IEEE float, so C's autocasting of doubles to floats will be bypassed.

orz
Member #565
August 2000

I think your problem is that you're mixing up "fix" and "fixed". "fix" is a C++ class that handles conversion automatically. "fixed" is a typedef to long. Try either this:

double fPI = 3.141592653589793;
fixed fDeltaV = ftofix(fPI);

textprintf_ex(sBuffer, font, 10, 10, makecol(255, 100, 200),-1, "fDeltaV=%f", fixtof(fDeltaV));

or this

double fPI = 3.141592653589793;
fix fDeltaV = fPI;

textprintf_ex(sBuffer, font, 10, 10, makecol(255, 100, 200),-1, "fDeltaV=%f", (double)fDeltaV);

edit: I probably didn't explain that well. "fix" is C++ only, and attempts to be clever. "fixed" is plain C, requires the use of macros, and goes bonkers if you make a mistake. You convert a floating point number to a "fixed", and assign the result to a "fix".
The process goes like:
fPi: (double) = 3.141592653589793

ftofix(fPI): (fixed) = (long int)(3.141592653589793 * 65536) = 205887
(precision loss, but number semi-intact)

fDeltaV: (fix) = ((long int)(205887 * 65536)) / 65536 = 608108544 / 65536.0 = 9279.0
(now in weird format, overflow occurs, integer portion of number dropped)

fixtof(fDeltaV): (double) = ((long int)9279.0) / 65536.0 = 0.1415863037109375
(number is converted back to sane format, but the information lost can't be recovered)

Julian Guarin
Member #5,787
April 2005
avatar

This is from the allegro manual:

Unfortunately the only advantage of fixed point math routines is that you don't require a floating point coprocessor to use them. This was great in the time period of i386 and i486 machines, but stopped being so useful with the coming of the Pentium class of processors. From Pentium onwards, CPUs have increased their strength in floating point operations, equaling or even surpassing integer math performance.

by the way is "fixed" not "fix"

against what are you, hard to know it is,
but which side are you, yet harder.

"You've been neglecting my chickens again, Gwydion. Feed them, and quickly!."

Johannes Bergmark
Member #4,867
July 2004
avatar

Thanks for the nice replies! I'm been working quite much this week, so I will feel sorry if I don't remember so good, but what of I remember it seems that the fixed type is now obsolete. Because I got some type off error message when declaring fixed. Anyway, I'll check it tonight when I come home and reports back.

Anyway, for the time now I'm using the float-type for some of my variables. Though I'm a little unsure of what is best to use right now. I've some calculations with the cos, sin functions etc.. What do you think is the best and safest way to calculate this? Should I use the fixed funcions (i.e. fixcos()), or should I use the old math.h -library, or something else?

/Johannes, with a dangerous and secret project going on ;)

"There are no Mines for the Sword." --?

"Det finns inga Minor för Svärdet." --?

gnolam
Member #2,030
March 2002
avatar

Keep everything in floating point and use the regular C library trig functions. Saves you trouble, is just as fast and makes for much cleaner code.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

orz
Member #565
August 2000

I agree with gnolam on that one. The only problem is that Allegro requires a few things in fixed point format (sprite rotations IIRC), but aside from that fixed point is rarely worth the hassle on modern CPUs.

Matt Smith
Member #783
November 2000

This is assuming you intend to run your game on a modern cpu.

AllegroDS is almost working :)

Go to: