[Main] Title=Using Floating Point Arithmetic [Top] 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:
float
, double
and
long double
without any trouble (in fact, these three types are the same,
there is no difference between them). The old-style type ti_float
still exists due to compatibility reasons, but now it is the same as float
.
float a, b, c; a = 2.854; b = 7; c = 1.5e-7;In other words, no more ugly FLT and FEXP macros! They still exist due to compatibility reasons, but their usage is very deprecated now.
p = (q + 3.15) / (q - r); x1 = (-b + sqrt (b * b - 4 * a * c)) / (2 * a); if (p < 3.0) q += 1e-3;This feature is probably the best present for TIGCC users...
a = sin (b); b = exp (1); c = log (3.19); a = sqrt (b + c) * sinh (a / 2.);As you can see from the second example, automatic promotion from integer constants to the floating point is also implemented.
'n'
is a variable of integer (short, long, signed, unsigned) type:
a = n; b = sqrt (n); c = b + n;You can also explicitely cast an integer value to a floating point one:
a = (float)n;The function flt which performs the promotion explicitely still exists, due to compatibility reasons.
n = a;where
'n'
is an integer, and 'a'
is a float, although it is better
to express your wish more explicitely using type cast as
n = (int)a;The function trunc which performs the truncation explicitely still exists due to compatibility reasons.
%f
,
%e
and %g
, as defined in ANSI C. So, the following is
completely legal:
printf ("sin(%f)=%f", a, sin(a)); printf ("%f", 3.14);Note that the second statement was not valid prior to release 0.9 of TIGCC!
#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:
ti_float
is now equal to ANSI type float
, but
bcd
is still a structure. If your program uses the
bcd
type (not very likely), you should probably change it to
float
to make the program work, because functions which expect a
float
type will not accept a structured type. Two macros called
float_to_bcd and
bcd_to_float have been introduced to provide more general
conversion if necessary.
float
type (and they
will continue to work with ti_float), but the second
group now only works with bcd structures. So, if you used
bcdadd
etc. in your program (not very likely), you should
probably change it to fadd
etc. to make the program work.
ti_float a; a.exponent = 0x4003; a.mantissa = 0x3284300000000000;Then the program will not work with the new compiler, because
ti_float
is not a
structure any more. Using a new macro bcd_var you can easily
get your program to work. All you need to do is to re-express the above statements as
ti_float a; bcd_var(a).exponent = 0x4003; bcd_var(a).mantissa = 0x3284300000000000;Note, however, that the bcd type is still a structure.