www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/04/18:31:36

From: Shawn Hargreaves <Shawn AT talula DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Allegro fixed math routines
Date: Tue, 4 Feb 1997 19:39:36 +0000
Organization: None
Distribution: world
Message-ID: <Yne1OUA4B59yEwpC@talula.demon.co.uk>
References: <Pine DOT HPP DOT 3 DOT 95 DOT 970204130243 DOT 29841F-100000 AT hphalle7i DOT informatik DOT tu-muenchen DOT de>
NNTP-Posting-Host: talula.demon.co.uk
MIME-Version: 1.0
Lines: 60
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Christian Tamasiu writes:
>I just hacked in a small program which uses the fixed math routines of
>allegro. However, some strange things are going on (at least thats what it
>seems to be for me): 
>Why doesnt, for example, " x = y + 24 " (both x and y fixed) work???
>However, " x = y + itofix(24) " worked. But even more strange using some
>other values, it worked without itofix. It is also sometimes need using
>*,/,- and sometimes not.
>Why???

This is the nature of fixed point math. The trouble is that the compiler
has no way of knowing they are fixed point values: it thinks they are
regular 32 bit integers.

The easy way to make it work is to switch to C++ and use the 'fix'
class, which overloads a lot of operators so it will behave exactly as
you expect.

To use fixed point numbers in C, you have to remember that they aren't
ints, and regular int operations won't work correctly. Given that x and
y are fixed variables, you can do the following:

  x = y + itofix(24);
        Fine - this works.

  x = y + 24;
        No good - the compiler has no way of telling that it has to 
        convert 24 to a fixed point value before adding it.

  x = y * 2;
        This is ok. The compiler will use a regular integer 
        multiplication, but this works because multiplying a fixed point 
        value by an integer gives a fixed point result.

  x = y * itofix(2);
        No good - the compiler will use regular integer multiplies, but 
        this isn't correct for fixed point values.

  x = fmul(y, itofix(2));
        Ok. If you want to multiply or divide two fixed point values, 
        you have to use special routines...

Basically you have to remember not to mix ints and fixed values in the
same statement, with a few exceptions:

When adding, subtracting, and comparing, you must't mix values.

You can multiply or divide a fixed by an int, and you will get a fixed
result.

To multiply or divide two fixed values, you have to use the fmul() and
fdiv() functions.

All this can be a big pain to keep track of - it's a _lot_ simpler to
use floats or the fix class in C++ :-)

/*
 *  Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/
 *  Ghoti: 'gh' as in 'enough', 'o' as in 'women', and 'ti' as in 'nation'.
 */

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019