Using Floating Point Arithmetic

If you have used earlier releases of TIGCC (prior to 0.9), you probably know that the use of floating point values and functions was possible, but only using some very awkward syntax, which caused a lot of headaches and nightmares. The TIGCC team (mostly Sebastian Reichelt and Zeljko Juric) spent a lot of time and effort implementing native (ANSI) floats in TIGCC. And the results are now available: you can use regular floating point numbers and values now, as in all regular C compilers! This means that:

As you can see, the usage of floats with TIGCC is now essentially the same as in all other C compilers. See the description of the math.h and timath.h header files for more info. However, floating point support in TIGCC is not perfect yet. That's why there are still some limitations in the use of floating point values (fortunately, they are not serious):

As an example of usage of floating point values and functions, the program given below (called "Float Test") reads coefficients of a quadratic equation from the keyboard, then calculates and displays the solutions of the equation (including complex ones):

#define USE_TI89
#define USE_TI92PLUS
#define USE_V200

#define SAVE_SCREEN

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <kbd.h>

void _main(void)
{
  float a, b, c, d;
  char buffer[200];
  clrscr ();
  puts ("a=");
  a = atof (gets (buffer));
  puts ("b=");
  b = atof (gets (buffer));
  puts ("c=");
  c = atof (gets (buffer));
  if (is_nan (a) || is_nan (b) || is_nan (c)) return;
  d = b * b - 4. * a * c;
  if (d >= 0.)
    {
      float x1, x2;
      x1 = (-b + sqrt (d)) / (2. * a);
      x2 = (-b - sqrt (d)) / (2. * a);
      printf ("\nx1=%f\nx2=%f", x1, x2);
    }
  else
    {
      float re, im;
      re = -b / (2. * a);
      im = fabs (sqrt (-d) / (2. * a));
      printf ("\nx1=%f-%f*i\nx2=%f+%f*i", re, im, re, im);
    }
  ngetchx();
}

See the description of the included header files for more info about the functions used.

As already mentioned above, the new floating point support is implemented without losing compatibility with programs written with releases of TIGCC before 0.9 (more precise, the degree of compatibility is about 95%; read further to see possible reasons of incompatibility). So, the quadratic equation solver given below, which is written using old methods (prior to TIGCC 0.9), will still work with a new compiler. Compare this (old-style) code with the previous (new-style) one to see how much clearer the new-style code is...

#define SAVE_SCREEN

#include <stdio.h>
#include <timath.h>
#include <string.h>
#include <kbd.h>

int _ti89, _ti92plus;

void _main (void)
{
  ti_float a, b, c, d;
  char buffer[200];
  clrscr ();
  puts ("a=");
  a = atof (gets (buffer));
  puts ("b=");
  b = atof (gets (buffer));
  puts ("c=");
  c = atof (gets (buffer));
  if (is_nan (a) || is_nan (b) || is_nan (c)) return;
  d = fsub (fmul (b, b), fmul (FLT (4), fmul (a, c)));
  if (fcmp (d, ZERO) >= 0)
    {
      ti_float x1, x2;
      x1 = fdiv (fadd (fneg (b), sqrt (d)), fadd (a, a));
      x2 = fdiv (fsub (fneg (b), sqrt (d)), fadd (a, a));
      printf ("\nx1=%f\nx2=%f", x1, x2);
    }
  else
    {
      ti_float re, im;
      re = fdiv (fneg (b), fadd (a,a));
      im = fabs (fdiv (sqrt (fneg (d)), fadd(a, a)));
      printf ("\nx1=%f-%f*i\nx2=%f+%f*i", re, im, re, im);
    }
  ngetchx();
}

The possible reasons which may cause incompatibility (very unlikely) with programs written with older versions of TIGCC (prior to 0.9) are:


Return to the main index