Routines for floating point arithmetic
Note: All functions which return a result of type float are implemented as macros, although many of them exist as TIOS entries. This is done because the GCC convention for returning floating point values as function results differs from the convention expected by the TIOS. This note is mainly unimportant from the user's point of view.
See also: math.h
float acos (float x); |
Floating point arc cosine.
acos returns the arc cosine of floating point argument x. The result is
always in radians.
Note: If the argument is not in range from -1 to 1, acos will return
NAN.
float acosh (float x); |
Floating point hyperbolic area cosine.
asinh returns the hyperbolic area cosine of floating point argument x,
Hyperbolic area cosine is defined as log(x+sqrt(x*x-1)).
Note: acosh will return NAN if x is smaller than 1.
float asin (float x); |
Floating point arc sine.
asin returns the arc sine of floating point argument x. The result is
always in radians.
Note: If the argument is not in range from -1 to 1, asin will return
NAN.
float asinh (float x); |
Floating point hyperbolic area sine.
asinh returns the hyperbolic area sine of floating point argument x. Hyperbolic area sine is defined as log(x+sqrt(x*x+1)).
float atan2 (float y, float x); |
Four-quadrant arc tangent of y/x (or argument of the complex number).
atan2 returns the four-quadrant arc tangent of y/x. More precise,
it returns the argument of the complex number x + y i.
So, the result is in the range -pi to pi.
Note: atan2 produces correct results even when the
resulting angle is near pi/2 or -pi/2 (x near
zero). If both x and y are zeros, atan2 returns NAN.
float atan (float x); |
Floating point arc tangent.
asin returns the arc tangent of floating point argument x. The result is always in radians.
float atanh (float x); |
Floating point hyperbolic area tangent.
atanh returns the hyperbolic area tangent of floating point argument x.
The hyperbolic area tangent is defined as log((1+x)/(1-x))/2.
Note: atanh will return NAN if x is smaller than -1
or greater than 1. Also, it will return POSITIVE_INF
if x is 1, and NEGATIVE_INF if x is -1.
float atof (const char *s); |
Converts a string to a floating point.
atof converts a string pointed to by s to floating point value. It recognizes the character representation of a floating point number, made up of the following:
an optional string of spaces;
an optional minus sign;
a string of digits and an optional decimal point (the digits can be on both sides of the decimal point);
an optional exponent followed by a (optionally signed) integer.
It is important to say that this implementation of atof requires that an optional minus sign and an optional exponent must be TI Basic characters for minus sign and exponent, (characters with codes 0xAD and 0x95 instead of ordinary '-' and 'e' or 'E' characters). This limitation is caused by using some TIOS calls which needs such number format. Anyway, it is very easy to "preprocess" any string to satisfy this convention before calling to atof by routine like the following (assuming that c is a char variable, and i is an integer variable):
for (i = 0; (c = s[i]); i++) // Yes, the second '=' is really '=', not '=='... { if (c == '-') s[i] = 0xAD; if ((c|32) == 'e') s[i] = 0x95; }
atof returns the converted value of the input string. It returns NAN if the
input string cannot be converted (i.e. if it is not in a correct format). This is not the same
as in ANSI C: atof in ANSI C returns 0 if the conversion was not successful. I decided to
return NAN instead, so the user can check whether the conversion was
successful (which is not possible with ANSI atof). See is_nan for a good
method to check whether the result is NAN.
Note: This function is not part of TIOS, and it is implemented
using the TIOS function push_parse_text.
Note: You should use strtod instead of atof, since it will take up less space in
your program, and provide a more direct and finer error-checking.
See also: strtod
float bcd_to_float (bcd x); |
Converts BCD to float.
bcd_to_float converts BCD structure x into an ordinary floating point value.
In fact, this function returns the same value as the argument, but with different interpretation.
This function, in a way, performs typecasting from a bcd type to an ordinary
float
type.
#define bcd_var(a) (*(bcd*)&(a)) |
Converts reference to float object to reference to BCD object.
bcd_var converts the reference to a floating point object x (which must be an lvalue, for example a floating point variable) to the reference to the same object, but interpreted as a bcd structure. bcd_var is similar as float_to_bcd, but returned object is an lvalue, so it may be used in assignments like
float a; ... bcd_var(a).exponent = 0x4002;
The drawback of bcd_var (compared with float_to_bcd) is the fact
that its argument must be an lvalue, so it cannot be a floating point constant or any
non-lvalue expression (for example, a result of a function).
Note: I used notation "&bcd_var" and "&x" in the prototype description, although passing
by reference and returning results by reference does not exist in ordinary C (only in C++).
However, this bcd_var is macro which is implemented on such way that it simulates
"passing and returning by reference".
bcd bcdadd (bcd x, bcd y); |
BCD addition.
bcdadd is principally the same as fadd, but instead of ordinary floats, its arguments and return value are bcd structures (which represent internal organization of floating point values in TIOS). At the fundamental level, bcdadd and fadd are the same routine.
bcd bcdbcd (long x); |
Converts integer to BCD.
bcdbcd is principally the same as flt, but instead of ordinary float, its return value is a bcd structure (which represent internal organization of floating point values in TIOS). At the fundamental level, bcdbcd and flt are the same routine.
long bcdcmp (bcd x, bcd y); |
BCD comparation.
bcdcmp is principally the same as fcmp, but instead of ordinary floats, its arguments are bcd structures (which represent internal organization of floating point values in TIOS). At the fundamental level, bcdcmp and fcmp are the same routine.
bcd bcddiv (bcd x, bcd y); |
BCD division.
bcddiv is principally the same as fdiv, but instead of ordinary floats, its arguments and return value are bcd structures (which represent internal organization of floating point values in TIOS). At the fundamental level, bcddiv and fdiv are the same routine.
long bcdlong (bcd x); |
Converts BCD to integer.
bcdlong is principally the same as trunc, but instead of ordinary float, its argument is a bcd structure (which represent internal organization of floating point values in TIOS). At the fundamental level, bcdlong and trunc are the same routine.
bcd bcdmul (bcd x, bcd y); |
BCD multiplication.
bcdmul is principally the same as fmul, but instead of ordinary floats, its arguments and return value are bcd structures (which represent internal organization of floating point values in TIOS). At the fundamental level, bcdmul and fmul are the same routine.
bcd bcdneg (bcd x); |
BCD negation.
bcdneg is principally the same as fneg, but instead of ordinary floats, its argument and return value are bcd structures (which represent internal organization of floating point values in TIOS). At the fundamental level, bcdneg and fneg are the same routine.
bcd bcdsub (bcd x, bcd y); |
BCD substraction.
bcdsub is principally the same as fsub, but instead of ordinary floats, its arguments and return value are bcd structures (which represent internal organization of floating point values in TIOS). At the fundamental level, bcdsub and fsub are the same routine.
void cacos (float z_re, float z_im, float *w_re, float *w_im); |
Complex arc cosine.
cacos calculates the arc cosine w = acos(z) of the complex number which real and
imaginary parts are z_re and z_im, and stores real and
imaginary part of the result in floating point destinations pointed to by
w_re and w_im. The complex arc cosine is defined by
acos(z) = -i ln (z + i sqrt (1 - z^2))
where ln and sqrt are complex natural
logarithm and complex square root (see cln and
csqrt).
void cacosh (float z_re, float z_im, float *w_re, float *w_im); |
Complex hyperbolic area cosine.
cacosh calculates the hyperbolic area cosine w = acosh(z) of the complex number which real and
imaginary parts are z_re and z_im, and stores real and
imaginary part of the result in floating point destinations pointed to by
w_re and w_im. The complex hyperbolic area cosine is defined by
acosh(z) = ln (z + sqrt (z^2 - 1))
where ln and sqrt are complex natural
logarithm and complex square root (see cln and
csqrt).
void casin (float z_re, float z_im, float *w_re, float *w_im); |
Complex arc sine.
casin calculates the arc sine w = asin(z) of the complex number which real and
imaginary parts are z_re and z_im, and stores real and
imaginary part of the result in floating point destinations pointed to by
w_re and w_im. The complex arc sine is defined by
asin(z) = -i ln (i z + sqrt (1 - z^2))
where ln and sqrt are complex natural
logarithm and complex square root (see cln and
csqrt).
void casinh (float z_re, float z_im, float *w_re, float *w_im); |
Complex hyperbolic area sine.
casinh calculates the hyperbolic area sine w = asinh(z) of the complex number which real and
imaginary parts are z_re and z_im, and stores real and
imaginary part of the result in floating point destinations pointed to by
w_re and w_im. The complex hyperbolic area sine is defined by
asinh(z) = ln (z + sqrt (z^2 + 1))
where ln and sqrt are complex natural
logarithm and complex square root (see cln and
csqrt).
void catan (float z_re, float z_im, float *w_re, float *w_im); |
Complex arc tangent.
catan calculates the arc tangent w = atan(z) of the complex number which real and
imaginary parts are z_re and z_im, and stores real and
imaginary part of the result in floating point destinations pointed to by
w_re and w_im. The complex arc tangent is defined by
atan(z) = -i ln ((1 + i z) / (1 - i z)) / 2
where ln is complex natural
logarithm (see cln).
void catanh (float z_re, float z_im, float *w_re, float *w_im); |
Complex hyperbolic area tangent.
catanh calculates the hyperbolic area tangent w = atanh(z) of the complex number which real and
imaginary parts are z_re and z_im, and stores real and
imaginary part of the result in floating point destinations pointed to by
w_re and w_im. The complex hyperbolic area tangent is defined by
atanh(z) = ln ((1 + z) / (1 - z)) / 2
where ln is complex natural logarithm
(see cln).
void ccos (float z_re, float z_im, float *w_re, float *w_im); |
Complex cosine.
ccos calculates the cosine w = cos(z) of the complex number which real and
imaginary parts are z_re and z_im, and stores real and
imaginary part of the result in floating point destinations pointed to by
w_re and w_im. The complex cosine is defined by
cos(z) = (exp(i z) + exp(-i z)) / 2
where exp is complex exponential function
(see cexp).
void ccosh (float z_re, float z_im, float *w_re, float *w_im); |
Complex hyperbolic cosine.
ccosh calculates the hyperbolic cosine w = cosh(z) of the complex number which real and
imaginary parts are z_re and z_im, and stores real and
imaginary part of the result in floating point destinations pointed to by
w_re and w_im. The complex hyperbolic cosine is defined by
cosh(z) = (exp(z) + exp(-z)) / 2
where exp is complex exponential function
(see cexp).
float ceil (float x); |
Rounds up the floating point number.
ceil finds the smallest integer not less than floating point argument x, and returns the integer found as a floating point value.
void cexp (float z_re, float z_im, float *w_re, float *w_im); |
Complex exponential function.
cexp calculates the complex exponential function w = exp(z) of the complex
number which real and imaginary parts are z_re and
z_im, and stores real and imaginary part of the result
in floating point destinations pointed to by w_re and
w_im. The complex exponential function is defined by
exp(z) = exp(z_re) (cos(z_im) + i sin(z_im))
See exp, sin and cos.
short ck_valid_float (float *ptr); |
Checks if the float pointed to by ptr is valid.
This function first rounds *ptr to 14 significant digits and underflows it to 0 if the exponent is less than -999 (it returns TRUE in that case). If the float pointed to by ptr is transfinite (see is_float_transfinite) or its exponent is greater than +999, *ptr is set to NAN and FALSE is returned. Else, TRUE is returned.
void cln (float z_re, float z_im, float *w_re, float *w_im); |
Complex natural logarithm (base e).
cln calculates the natural logarithm w = ln(z) of the complex
number which real and imaginary parts are z_re and
z_im, and stores real and imaginary part of the result
in floating point destinations pointed to by w_re and
w_im. The complex logarithm is defined by
ln(z) = log(abs(z)) + i arg(z)
where abs(z) = sqrt(z_re^2+z_im^2), arg(z) = atan2(z_im, z_re)
and log is the real natural logarithm. See
also sqrt and atan2.
void clog10 (float z_re, float z_im, float *w_re, float *w_im); |
Complex logarithm, base 10.
clog10 calculates the base 10 logarithm w = log10(z) of the complex
number which real and imaginary parts are z_re and
z_im, and stores real and imaginary part of the result
in floating point destinations pointed to by w_re and
w_im. The base 10 complex logarithm is defined by
log10(z) = ln(z) / ln(10)
where ln is complex natural logarithm
(see cln).
float cos (float x); |
Floating point cosine.
cos returns the cosine of floating point argument x, which is assumed
to be specified in radians.
Note: cos will return NAN if the argument is so big that
reducing to the main period can't be performed without complete losing of
significant digits (i.e. when the magnitude of x is greater than 1e13).
float cosh (float x); |
Floating point hyperbolic cosine.
sinh returns the hyperbolic cosine of floating point argument x.
Hyperbolic cosine is defined as (exp(x)+exp(-x))/2.
Note: cosh will return POSITIVE_INF
in a case of overflow.
void csin (float z_re, float z_im, float *w_re, float *w_im); |
Complex sine.
csin calculates the sine w = sin(z) of the complex number which real and
imaginary parts are z_re and z_im, and stores real and
imaginary part of the result in floating point destinations pointed to by
w_re and w_im. The complex sine is defined by
sin(z) = (exp(i z) - exp(-i z)) / (2 i)
where exp is complex exponential function
(see cexp).
void csinh (float z_re, float z_im, float *w_re, float *w_im); |
Complex hyperbolic sine.
csinh calculates the hyperbolic sine w = sinh(z) of the complex number which real and
imaginary parts are z_re and z_im, and stores real and
imaginary part of the result in floating point destinations pointed to by
w_re and w_im. The complex hyperbolic sine is defined by
sinh(z) = (exp(z) - exp(-z)) / 2
where exp is complex exponential function
(see cexp).
void csqrt (float z_re, float z_im, float *w_re, float *w_im); |
Complex square root.
csqrt calculates the square root w = sqrt(z) of the complex number which real and
imaginary parts are z_re and z_im, and stores real and
imaginary part of the result in floating point destinations pointed to by
w_re and w_im. The complex square root is defined by
sqrt(z) = sqrt(abs(z)) (cos(arg(z)/2) + i sin(arg(z)/2))
where abs(z) = sqrt(z_re^2+z_im^2) and arg(z) = atan2(z_im, z_re).
See sqrt, atan2,
sin and cos.
void ctan (float z_re, float z_im, float *w_re, float *w_im); |
Complex tangent.
ctan calculates the tangent w = tan(z) of the complex number which real and
imaginary parts are z_re and z_im, and stores real and
imaginary part of the result in floating point destinations pointed to by
w_re and w_im. The complex tangent is defined by
tan(z) = sin(z) / cos(z)
where sin and cos are complex sine and
complex cosine (see csin and ccos).
void ctanh (float z_re, float z_im, float *w_re, float *w_im); |
Complex hyperbolic tangent.
ctanh calculates the hyperbolic tangent w = tanh(z) of the complex number which real and
imaginary parts are z_re and z_im, and stores real and
imaginary part of the result in floating point destinations pointed to by
w_re and w_im. The complex hyperbolic tangent is defined by
tanh(z) = sinh(z) / cosh(z)
where sinh and cosh are complex hyperbolic
sine and complex hyperbolic cosine (see csinh and
ccosh).
float exp (float x); |
Floating point exponential function.
exp returns the exponential function of floating point argument x
(i.e. calculates e to the x-th power).
Note: exp will return POSITIVE_INF in a case of overflow, and
zero (unsigned, see ZERO; strange, I expected
POSITIVE_ZERO) in a case of underflow.
float fabs (float x); |
Absolute value of a floating point number.
fabs returns the absolute value of floating point argument x.
float fadd (float x, float y); |
Floating point addition.
fadd returns the sum of floating point arguments x and y.
This routine performs the same operation as the C '+'
operator applied to floating
point operands, but it is kept here for backwards compatibility.
At the fundamental level, fadd is exactly the same routine as
bcdadd.
long fcmp (float x, float y); |
Floating point comparation.
fcmp compares floating point arguments x and y, and returns a value which is
< 0 if x is less than y
== 0 if x is the same as y
> 0 if x is greater than y
This function may be useful as a comparison function for qsort
function from stdlib.html. All relation operators applied to
floating point types are implemented through implicite calls of this function.
At the fundamental level, fcmp is exactly the same routine as
bcdcmp.
Note: All kind of zeros are equal from the aspect of comparation. Transfinite values
are incomparable, and the result of fcmp is unpredictable (usually 1, but this is not
guarantee) if any argument is transfinite. See ZERO,
UNSIGNED_ZERO, POSITIVE_ZERO,
NEGATIVE_ZERO, UNSIGNED_INF,
POSITIVE_INF, NEGATIVE_INF
and NAN for more details.
float fdiv (float x, float y); |
Floating point division.
fdiv returns the quotient of floating point arguments x and y.
This routine performs the same operation as the C '/'
operator applied to floating
point operands, but it is kept here for backwards compatibility.
At the fundamental level, fdiv is exactly the same routine as
bcddiv.
Note: fdiv returns infinite result
if the argument is zero (signed or unsigned), or in a case of overflow.
Also, it returns NAN if both arguments are zeros
or infinities.
See ZERO, UNSIGNED_ZERO,
POSITIVE_ZERO, NEGATIVE_ZERO,
UNSIGNED_INF, POSITIVE_INF and
NEGATIVE_INF for more details.
A deprecated macro used to define floating point numbers.
FEXP_NEG (also deprecated) works exactly like FEXP, except it constructs negative values instead of positive ones.
A deprecated macro used to define floating point numbers.
Yet one deprecated macro. FEXP(m,e)
constructs
a number m*10^e
where m is a sequence
of digits (without decimal point) which is assumed to represent decimal number
m.mmmm..., e.g. FEXP(2514,5)
represents number
2.514*10^5
(251400
or 2.514e5
using
conventional exponential notation), and FEXP(42,-3)
represents
number 4.2*10^-3
(0.0042
or 4.2e-3
).
FEXP(1,3)
is 1*10^3
(1000
or 1e3
).
mantissa must be the constant sequence of digits, without leading zeros,
but the way on which FEXP
is implemented allows that
exponent may be a variable or an expression, like
FEXP(314,a)
, when even mantissa is not a constant, you
can use function ldexp10. Anyway, you don't need to
use FEXP
any more: simply use conventional exponential notation.
E.g. simply use 4.2e3
instead of FEXP(42,3)
etc.
Note that a = FEXP(m,e)
is not the same as
bcd_var(a).exponent = e+0x4000
and bcd_var(a).mantissa = m
. The first part is
true; the second is not. More precise, FEXP
shifts m to
the left enough number of times to produce correct normalized mantissa (see
bcd for more info).
So, when you type a = FEXP(352,3)
it works like
bcd_var(a).exponent = 0x4003
and bcd_var(a).mantissa = 0x3520000000000000
.
For more description about internal format of
floating point numbers, see bcd.
See also: float_to_bcd, bcd_to_float, bcd_var
short float_class (float x); |
Determines the class of the floating point number.
float_class checks the floating point argument x and returns an integer
result which determines the subtype of the argument, in according to the
following table:
1 | Not_a_Number (NAN) |
2 | Negative infinity (NEGATIVE_INF) |
3 | Negative real number |
5 | Negative zero (NEGATIVE_ZERO) |
6 | Unsigned zero (ZERO) |
7 | Positive zero (POSITIVE_ZERO) |
9 | Positive real number |
10 | Positive infinity (POSITIVE_INF) |
11 | Unsigned infinity (UNSIGNED_INF) |
bcd float_to_bcd (float x); |
Converts float to BCD.
bcd_to_float converts BCD structure x into an ordinary floating point value.
In fact, this function returns the same value as the argument, but with different interpretation.
This function, in a way, performs typecasting from an ordinary float
type to a
bcd type. Beware that returned value is not an lvalue (ordinary C functions
never return lvalues, by the way), so you cannot do something like
float a; ... float_to_bcd(a).exponent = 0x4002;
To perform such assignments, use bcd_var macro.
float floor (float x); |
Rounds down the floating point number.
floor finds the largest integer not greater than floating point argument x, and returns the integer found as a floating point value.
A deprecated macro used to define floating point numbers.
FLT_NEG is another deprecated macro which works exactly as
FLT, except it construct negative values
instead of positive ones. More precise, FLT_NEG(x,y)
will simply be
translated to -x.y
and FLT_NEG(x)
will be translated to
-x.0
. Anyway, you don't need to use the FLT_NEG
macro any
more.
float flt (long x); |
Converts integer to floating point.
flt converts the (long) integer argument x to the floating point representation
of the same value. This routine performs the same operation as casting a long integer
value to float type using the '(float)'
typecast operator,
but it is kept here for backwards compatibility.
This function is automatically called when any floating point function is called with
a long integer argument, to force a promotion of an integer to a floating
point type. In other words, integer values will be automatically promoted to the floating
point type when necessary. So, you can do assignment like b = a;
when a is an integer variable (or expression) and b is a standard floating
point variable (float or double). Also, you can calculate
sin(a) where a is an integer. In both cases, flt will be called automatically
to perform the promotion. At the fundamental level, flt is exactly the same routine as
bcdbcd.
A deprecated macro used to define floating point numbers.
FLT is a now deprecated macro which is kept here only to retain compatibility
with programs which are written with older releases of TIGCC (before 0.9),
which didn't support standard floating point numbers. Now,
FLT(x,y)
will simply be translated to x.y
and
FLT(x)
will be translated to x.0
, e.g.
FLT(342,15)
will be translated to 342.15
,
FLT(0,0001)
will be translated to 0.0001
, and
FLT(4)
will be translated to 4.0
. Anyway, you don't
need to use the FLT
macro any more.
float fmod (float x, float y); |
Calculates x modulo y, i.e. the remainder of x/y.
fmod returns x modulo y, i.e. it returns the remainder f, where x = a * y + f for some integer a and 0 <= f < y. Where y = 0, fmod returns 0.
float fmul (float x, float y); |
Floating point multiplication.
fmul returns the product of floating point arguments x and y.
This routine performs the same operation as the C '*'
operator applied to floating
point operands, but it is kept here for backwards compatibility.
At the fundamental level, fmul is exactly the same routine as
bcdmul.
Note: fmul returns infinite result
in a case of overflow. See UNSIGNED_INF,
POSITIVE_INF and NEGATIVE_INF for
more details.
float fneg (float x); |
Floating point negation.
fneg returns negated value of floating point argument x.
This routine performs the same operation as the C '-'
unary minus operator applied
to a floating point operand, but it is kept here for backwards compatibility.
At the fundamental level, fneg is exactly the same routine as
bcdneg.
short fpisanint (unsigned long long *mantissa, unsigned short exponent); |
Checks whether the floating point number is reducable to an integer.
fpisanint is an internal TIOS subroutine used in pow.
It checks whether the floating point with the exponent exponent
and the mantissa pointed to by mantissa is reducable to an integer.
Returns TRUE or FALSE, depending
on the test.
Note: long long is not a typing error: it is a
GNU C extension for representing very long integers (8-byte integers in this implementation).
short fpisodd (const unsigned long long *mantissa, unsigned short exponent); |
Checks whether the integer part of a floating point number is an odd number.
fpisodd is an internal TIOS subroutine used in pow.
It checks whether the integer part of the floating point with the
exponent exponent and the mantissa pointed to by mantissa is
an odd number. Returns TRUE or FALSE,
depending on the test. Also returns TRUE if the integer
part is zero, although zero is not an odd number.
Note: long long is not a typing error: it is a
GNU C extension for representing very long integers (8-byte integers in this implementation).
float frexp10 (float x, short *exponent); |
Splits floating point number into mantissa and exponent.
frexp10 calculates the mantissa m (a floating point greater than or
equal to 0.1 and less than 1) and the integer value n, such that x
equals m*10^n. frexp stores n in the integer that exponent
points to, and returns the mantissa m.
Note: This routine is analogous to frexp
in ANSI C math library, except
using base ten rather than base two.
float fsub (float x, float y); |
Floating point substraction.
fsub returns the difference of floating point arguments x and y.
This routine performs the same operation as the C '-'
operator applied to floating
point operands, but it is kept here for backwards compatibility.
At the fundamental level, fsub is exactly the same routine as
bcdsub.
float hypot (float x, float y); |
Calculates hypotenuse of right triangle.
hypot returns the value z where z^2 = x^2 + y^2 and
z >= 0. This is equivalent to the length of the hypotenuse of a right
triangle, if the lengths of the two sides are x and y. Or, this
is also equivalent to the absolute value of the complex number
x + y i.
Note: hypot is implemented as macro which calls fmul (for squaring
x and y), fadd and sqrt.
void init_float (void); |
Initializes the floating point emulator.
init_float initializes the TIOS floating point emulator. However, as far as I know,
you need not to call this function explicitely anywhere in your program, because TIOS
does it in the boot code. As this function is removed from AMS 2.xx, it is redefined
in this release of TIGCCLIB to do nothing (it is kept in this header file only for
compatibility with previous releases).
Note: TIOS fp emulator is, in fact, located in TIOS routine called _bcd_math.
This routine performs a set of arithmetic operations on binary coded decimal floating
point values. A two-byte emulator opcode word is needed after the call to _bcd_math
to instruct the emulator which operation will be performed, where are its operands, etc.
Operands may be in MC68000 registers, in the memory, or in "floating point registers"
(they are, in fact, memory locations on the stack frame from the aspect of the emulator).
If the "instruction" has immediate operand, there will be more than one extra inline word
after the call to _bcd_math.
Anyway, function _bcd_math is unusable in C programs (except
in asm statements), due to non-C calling convention. Fortunately, nearly
all operations supported by _bcd_math have also a built-in interface
in TIOS which is adapted to C calling convention.
That's why I completely bypassed _bcd_math function in this header
file. It may give additional flexibility for ASM programmers (sometimes the argument may be in
the register, sometimes in the memory, etc.). But in C, the arguments are always on the stack,
so this flexibility is not necessary.
short is_float_infinity (float x); |
Checks whether the argument is an infinite number.
is_float_infinity is original TIOS name for the function which is aliased here as is_inf.
short is_float_negative_zero (float x); |
Checks whether the argument is negative zero.
is_float_negative_zero is original TIOS name for the function which is aliased here as is_nzero.
short is_float_positive_zero (float x); |
Checks whether the argument is positive zero.
is_float_positive_zero is original TIOS name for the function which is aliased here as is_pzero.
short is_float_signed_infinity (float x); |
Checks whether the argument is signed infinity.
is_float_signed_infinity is original TIOS name for the function which is aliased here as is_sinf.
short is_float_transfinite (float x); |
Checks whether the argument is a transfinite number.
is_float_transfinite is original TIOS name for the function which is aliased here as is_transfinite.
short is_float_unsigned_inf_or_nan (float x); |
Checks whether the argument is unsigned infinity or Not_a_Number.
is_float_unsigned_inf_or_nan is original TIOS name for the function which is aliased here as is_uinf_or_nan.
short is_float_unsigned_zero (float x); |
Checks whether the argument is unsigned zero.
is_float_unsigned_zero is original TIOS name for the function which is aliased here as is_uzero.
short is_inf (float x); |
Checks whether the argument is an infinite number.
is_inf returns TRUE if x is an infinite number (i.e. UNSIGNED_INF, POSITIVE_INF or NEGATIVE_INF), else returns FALSE. Infinite values are produced when the result is unbounded (for example dividing by zero), or in case of overflow. This function is an alias for TIOS function originally called is_float_infinity.
short is_nan (float x); |
Checks whether the argument is Not_a_Number.
is_nan returns TRUE if x is NAN (Not_a_Number), else returns FALSE. Not_a_Number is a special value which is produced as a result of all operations when the result is undefined or non-real, for example dividing zero with zero, calculating the logarithm of a negative number, etc.
short is_nzero (float x); |
Checks whether the argument is negative zero.
is_nzero returns TRUE if x is a negative zero (i.e. infinitely small negative quantity, see NEGATIVE_ZERO), else returns FALSE. This function is an alias for TIOS function originally called is_float_negative_zero.
short is_pzero (float x); |
Checks whether the argument is positive zero.
is_pzero returns TRUE if x is a positive zero (i.e. infinitely small positive quantity, see POSITIVE_ZERO), else returns FALSE. This function is an alias for TIOS function originally called is_float_positive_zero.
short is_sinf (float x); |
Checks whether the argument is signed infinity.
is_sinf returns TRUE if x is a signed infinity (i.e. POSITIVE_INF or NEGATIVE_INF), else returns FALSE. This function is an alias for TIOS function originally called is_float_signed_infinity.
short is_transfinite (float x); |
Checks whether the argument is a transfinite number.
is_transfinite returns TRUE if x is a transfinite number, else returns FALSE. Transfinite numbers are all infinite numbers (UNSIGNED_INF, POSITIVE_INF and NEGATIVE_INF) and NAN. This function is an alias for TIOS function originally called is_float_transfinite.
short is_uinf_or_nan (float x); |
Checks whether the argument is unsigned infinity or Not_a_Number.
is_uinf_or_nan returns TRUE if x is UNSIGNED_INF or NAN, else returns FALSE. These two special numbers are treated very similarly in TIOS. This function is an alias for TIOS function originally called is_float_unsigned_inf_or_nan.
short is_uzero (float x); |
Checks whether the argument is unsigned zero.
is_uzero returns TRUE if x is an unsigned zero (i.e. infinitely small quantity with indeterminate sign, see UNSIGNED_ZERO), else returns FALSE. This function is an alias for TIOS function originally called is_float_unsigned_zero.
void itrig (short option, short deg_flag, float *xptr, float *result); |
Generic subroutine for calculating inverse trigonometric functions.
itrig is a TIOS subroutine which is used internally for calculating
inverse trigonometric functions, i.e. in TIOS functions
asin, acos and
atan. It calculates the arc sine, the arc cosine or
the arc tangent of the floating point value pointed to by xptr, and stores the result in
the floating point destination pointed to by result.
The result will be in radians if deg_flag is 0, or in degrees if
deg_flag is 1 (it seems that these two values are only legal values
for deg_flag). Parameter option determines which inverse
trigonometric function will be calculated: the arc sine if option = 1,
the arc cosine if option = 2 and the arc tangent if
option = 4. I don't know whether these values are the
only legal values for option, but I believe so.
Note: The parameter xptr is not a pointer to const
value. This means that the value pointed to by it may be changed. In normal
cases this would not appear, but this need not to be true if the structure pointed
to by xptr contains wrong values (for example, arguments out of the function
domain, unnormalized values, etc.).
float ldexp10 (float x, short exponent); |
Calculates x times 10 raised to exponent.
ldexp10 calculates x times 10 raised to exponent, and returns the
result, i.e. returns x*10^exponent. Strictly speaking, ldexp10 is
a macro, not a function.
Note: This routine is analogous to ldexp
in ANSI C math library, except
using base ten rather than base two.
float log (float x); |
Floating point natural logarithm (base e).
log returns the natural logarithm of floating point argument x.
Note: log will return NEGATIVE_INF if the argument is
zero, or NAN if the argument is negative.
float log10 (float x); |
Floating point logarithm, base 10.
log10 returns the base 10 logarithm of floating point argument x.
Note: log will return NEGATIVE_INF if the argument is
zero, or NAN if the argument is negative.
float modf (float x, float *ipart); |
Splits floating point value into integer and fraction part.
modf breaks the floating point value x into two parts: the integer and the fraction, both having the same sign as x. It stores the integer in a floating point destination pointed to by ipart and returns the fractional part of x.
float pow (float x, float y); |
Floating point power function.
pow returns x^y, x to the y (i.e. x raised to the
y-th power).
Note: pow will return an infinite result (see POSITIVE_INF,
NEGATIVE_INF, UNSIGNED_INF)
in a case of overflow. If both x and y are zeros, pow will return 1.
If x is negative, the correct result will be produced only if y can
be represented as a whole number, or as a fraction with odd denominator; otherwise,
pow will return a garbage (not NAN) which sometimes even not
satisfy the floating point BCD format (digits greater than 9 etc.), so be careful in
a case when x is negative!
float round12_err (float x, short error_code); |
Rounds the floating point number to 12 significant digits, throwing an error if unsuccessful.
round12_err is identical as round12, except it throws an error with code error_code if the absolute value of the argument is greater or equal than 10^1000, or if the argument is a transfinite number (see is_transfinite), and it rounds arguments whose absolute values are smaller than 10^-1000 to UNSIGNED_ZERO.
float round12 (float x); |
Rounds the floating point number to 12 significant digits.
round12 returns the value of the floating point argument x rounded to 12 significant digits. TIOS sometimes does such rounding, for example when TIOS updates coordinate values (xc, yc, etc.), during printing approximate results, or when TIOS stores a value to system variables like xmin, xmax etc. (strictly speaking, TIOS calls round12_err instead of round12 in such cases).
float round14 (float x); |
Rounds the floating point number to 14 significant digits.
round14 returns the value of the floating point argument x rounded to 14 significant digits. Also, arguments whose absolute values are greater or equal than 10^8192 are rounded to POSITIVE_INF or NEGATIVE_INF, and arguments whose absolute values are smaller than 10^-8192 are rounded to POSITIVE_ZERO or NEGATIVE_ZERO. TIOS always does such rounding before storing a floating point value to a variable.
float sin (float x); |
Floating point sine.
sin returns the sine of floating point argument x, which is assumed
to be specified in radians.
Note: sin will return NAN if the argument is so big that
reducing to the main period can't be performed without complete losing of
significant digits (i.e. when the magnitude of x is greater than 1e13).
void sincos (float x, short deg_flag, float *sine, float *cosine); |
Calculates both sine and cosine in one turn.
sincos calculates both the sine and the cosine of floating point argument x, and stores the results in floating point destinationss pointed to by sine and cosine. The argument x is assumed to be specified in radians if deg_flag is 0, or in degrees if deg_flag is 1 (it seems that these two values are only legal values for deg_flag). See also notes related to sin and cos.
float sinh (float x); |
Floating point hyperbolic sine.
sinh returns the hyperbolic sine of floating point argument x.
Hyperbolic sine is defined as (exp(x)-exp(-x))/2.
Note: sinh will return POSITIVE_INF or
NEGATIVE_INF in a case of overflow.
float sqrt (float x); |
Floating point square root.
sqrt returns the positive square root of floating point argument x.
Note: If the argument is negative, there will be no error, but the result
will be invalid.
float tan (float x); |
Floating point tangent.
tan returns the tangent of floating point argument x, which is assumed
to be specified in radians.
Note: tan will return UNSIGNED_INF for all arguments
for which the tangent is infinity. Also, it will return NAN if the
argument is so big that reducing to the main period can't be performed without
complete losing of significant digits (i.e. when the magnitude of x is
greater than 1e13).
float tanh (float x); |
Floating point hyperbolic tangent.
sinh returns the hyperbolic cosine of floating point argument x. Hyperbolic tangent is defined as sinh(x)/cosh(x).
void trig (short option, short deg_flag, const float *xptr, float *sine, float *cosine, float *tangent); |
Generic subroutine for calculating trigonometric functions.
trig is a TIOS subroutine which is used internally for calculating
trigonometric functions, i.e. in TIOS functions
sin, cos, sincos and
tan. It calculates simultaneously the sine, the cosine and
the tangent of the floating point value pointed to by xptr, and stores the results in
floating point destinations pointed to by sine, cosine and
tangent. The argument pointed to by xptr is assumed to be specified in radians
if deg_flag is 0, or in degrees if deg_flag is 1 (it seems that
these two values are only legal values for deg_flag). Parameter option
is not very clear to me: TIOS uses option = 1 in
sin and sincos, option = 2
in cos, and option = 4 in
tan. I don't know what is the difference between
option = 1 and option = 2, because both the
sine and the cosine are calculated regardless of the value of option.
I only noticed that the tangent will not be calculated if option is not
equal to 4.
Note: I included the description of this routine here only due to completeness: it
is more preferable to call particular trigonometric function instead.
long trunc (float x); |
Converts floating point to integer.
trunc truncates floating point argument x to the long integer result. Returns
zero in a case of overflow.
This routine performs the same operation as casting a floating point value
to an int type using '(int)'
, '(unsigned int)'
and
'(long)'
typecast operators, but it is kept here for backwards compatibility.
This function is automatically called when any function which needs an integer is called with
a floating point arguments, to force a truncation of a floating point value to
an integer. Also, you can do assignments like b = a;
when a is a floating point variable (or expression) and b is an
integer variable. In both cases, trunc will be called automatically
to perform the truncation.
At the fundamental level, trunc is exactly the same routine as
bcdlong.
#define FIVE (5.) |
A deprecated constant.
FIVE is a predefined floating point constant with value 5.0, defined to keep backwards compatibility.
#define FOUR (4.) |
A deprecated constant.
FOUR is a predefined floating point constant with value 4.0, defined to keep backwards compatibility.
#define HALF_PI (1.570796326794897) |
A constant with value PI/2.
HALF_PI is a predefined floating point constant which approximates PI/2 up to
16 significant digits, i.e. 1.570796326794897
.
Of course, it is the same as PI/2.0
.
#define HALF (0.5) |
A deprecated constant.
HALF is a predefined floating point constant with value 0.5, defined to keep backwards compatibility.
#define MINUS_ONE (-1.) |
A deprecated constant.
MINUS_ONE is a predefined floating point constant with value -1.0, defined to keep backwards compatibility.
A constant defining an undefined value.
NAN
is an acronyme of "Not a Number". TIOS generates NAN
when nothing can be deduced
about the magnitude of the result (for example, when dividing zero by zero, or when
substracting two infinities of the same sign). Also, TIOS generates NAN
when the
argument of a function is out of legal range, excluding values of the argument which
produces infinity results. For example, log will produce NAN
when the
argument is negative, but when the argument is zero, the result is
NEGATIVE_INF.
NAN
also belongs to the class of "transfinite" numbers (see is_transfinite).
Use is_nan to check whether a value is NAN
. This is a common
method to check in run time whether the arguments of the called math functions was legal.
See also: UNSIGNED_INF
Represents an infinitely large negative quantity.
NEGATIVE_INF
represents an infinitely large negative quantity. TIOS generates
NEGATIVE_INF
when the result is infinite in magnitude, but when it is known to be negative (for example,
log returns NEGATIVE_INF
when the argument is equal to zero).
Other properties of NEGATIVE_INF
are analogous like properties of
POSITIVE_INF.
See also: POSITIVE_INF, UNSIGNED_INF, NEGATIVE_ZERO, is_transfinite
Represents an infinitely small quantity which is known to be always non-positive.
NEGATIVE_ZERO
is similar to POSITIVE_ZERO, but it
represents an infinitely small quantity which is known to be always non-positive.
The properties of NEGATIVE_ZERO
are analog to the properties of
POSITIVE_ZERO. To check whether a value is a negative
zero, use is_nzero.
#define ONE (1.) |
A deprecated constant.
ONE is a predefined floating point constant with value 1.0, defined to keep backwards compatibility.
#define PI (3.141592653589793) |
An approximated value of pi.
PI is a predefined floating point constant which approximates pi up
to 16 significant digits, i.e. 3.141592653589793
.
Represents an infinitely large positive quantity.
POSITIVE_INF
represents an infinitely large positive quantity. TIOS generates POSITIVE_INF
when the result is infinite in magnitude, but when it is known to be positive (for example,
atanh returns POSITIVE_INF
when the argument is equal to 1).
TIOS also generates POSITIVE_INF
as the result of positive
overflow (i.e. when the result is positive and too big to be represented in
float
type), and as the result of rounding extremely big positive
numbers using round14 or round12_err.
TIOS allows much greater flexibility when working with "signed"
infinities than with UNSIGNED_INF. To check whether a value
is signed infinity, use is_sinf. POSITIVE_INF
belongs to the
class of "transfinite" numbers (see is_transfinite).
See also: NEGATIVE_INF, UNSIGNED_INF, POSITIVE_ZERO, is_transfinite
Represents an infinitely small quantity which is known to be always non-negative.
In opposite to UNSIGNED_ZERO, POSITIVE_ZERO
is an infinitely
small quantity which is known to be always non-negative. It can be imagined as "the
smallest positive real number", altough something like this does not exist in reality.
TIOS generates POSITIVE_ZERO
in cases when the result is zero, but it is known that the
result cannot be negative for any argument. For example, squaring of ZERO
using pow function will return POSITIVE_ZERO
, because the square is
always non-negative. The same is true for acosh when the argument
is equal to 1, etc.
TIOS also generates POSITIVE_ZERO
as the result of positive underflow (i.e. when the
result is positive, but too small to be represented in a float
type),
and as the result of rounding extremely small positive numbers using
round14 or round12_err.
To check whether a value is a positive zero, use is_pzero.
Dividing any finite strictly positive number by POSITIVE_INF
will produce POSITIVE_ZERO
as the result. Dividing any finite strictly positive number by
POSITIVE_ZERO gives POSITIVE_INF, and dividing any finite
strictly negative number by POSITIVE_ZERO
gives NEGATIVE_INF.
Note: Try in TI Basic '1/0'
and '1/0^2'
to see that
'0'
and '0^2'
are not strictly the same
for TIOS. Clever, isn't it?
#define TEN (10.) |
A deprecated constant.
TEN is a predefined floating point constant with value 10.0, defined to keep backwards compatibility.
#define THREE (3.) |
A deprecated constant.
THREE is a predefined floating point constant with value 3.0, defined to keep backwards compatibility.
#define TWO (2.) |
A deprecated constant.
TWO is a predefined floating point constant with value 2.0, defined to keep backwards compatibility.
Represents an infinite quantity.
UNSIGNED_INF
represents a quantity which is known to be infinite in magnitude, but
when nothing can be deduced about its sign. For example, dividing of non-zero number
with "standard" zero (i.e. with UNSIGNED_ZERO) or calculating
tangent of pi/2 will produce such value. TIOS mathematical functions are much
more limited in working with unsigned than with signed infinities (like
POSITIVE_INF). For example, arc tangent of POSITIVE_INF
is
well defined and equals to pi/2, but arc tangent of UNSIGNED_INF
is not unique determined.
Although UNSIGNED_INF
is a much more "concrete"
quantity than NAN, TIOS very often does not make any difference
between these two quantities. To check whether a value is an unsigned infinity or
NAN, use is_uinf_or_nan. If it is,
then you can use is_nan for checking whether a value is NAN,
and if it it not, it must be an unsigned infinity. UNSIGNED_INF
belongs to the
class of "transfinite" numbers (see is_transfinite).
See also: POSITIVE_INF, NEGATIVE_INF, NAN, is_transfinite
#define UNSIGNED_ZERO (0.) |
Represents an infinitely small quantity with indeterminate sign.
TIOS makes a difference between three types of zeros. UNSIGNED_ZERO
is "ordinary" zero,
i.e. infinitely small quantity with indeterminate sign. It is identical to
ZERO. Dividing any finite non-zero number by UNSIGNED_ZERO
will produce
UNSIGNED_INF.
All kind of zeros are equal when comparing using comparison operators or fcmp. To check
whether a value is an unsigned zero, use is_uzero.
#define ZERO (0.) |
A deprecated constant.
ZERO is a predefined floating point constant with value 0.0
.
ZERO
is the same as UNSIGNED_ZERO.
typedef struct {
|
Represents the internal organization of floating point numbers in the format recognized by the TIOS.
The bcd
type represents the internal organization of floating point numbers
in the format recognized by the TIOS (the so-called SMAP II BCD format).
Note that long long is not a typing error: it is a
GNU C extension for representing very long integers (8-byte integers in this implementation).
Here will be given the exact internal organization of floating point numbers. Magnitude of every
real number (except zero) can be represented as m*10^e, where e
(so-called exponent) is an integer, and m (so-called mantissa) is a real number
which satisfies condition 1 <= m < 10
(this is somewhat different convention than used in frexp10 function
which is derived from ANSI standard). e is
stored in the exponent field, and m in mantissa field of the
bcd
structure. Details of storing format are given below. You don't
need to know these details, but they are given here for anybody who needs to
know more about floats on TI.
Field exponent of the bcd
structure contains e+0x4000
if the number is positive, or e+0xC000 if the number is negative. So, the most
significant bit of exponent is the sign of the number, but the format is not
2-complement code (more precise, it is sign_and_magnitude_0x4000_biased code). The exponent
is NOT bcd-coded (unlike the mantissa). Legal range for the e is from -16383 to +16382
(values -16384 and +16383 are reserved for some special values), although many math functions
are not very happy with extremely small or extremely big exponents. Keep your exponents in
the range from -999 to +999.
The mantissa is stored in BCD code. As the mantissa satisfies the condition
1 <= m < 10, it can be represented as
m1.m2m3m4... where m1, m2 etc. are digits (0-9).
TIOS first truncates the mantissa up to 16 digits, or adds trailing zeros on the
end of the mantissa up to 16 digits if it is shorter than 16 digits. Then,
it stores the integer number m1m2m3...m16 in mantissa field
of the bcd
structure using packed BCD code (each digit in
4 bits).
Everything will be more clear on a concrete example. Look the number 379.25
. It can
be written as 3.7925*10^2
. So, e is 2
, and m is 3.7925
. As the
number is positive, exponent will contain 0x4000+2 = 0x4002
.
As the mantissa has less than 16 digits, it must be padded to 3.792500000000000
.
So, mantissa will contain the integer 3792500000000000
, i.e. it will contain
0x3792500000000000
(note the strong correspodence between hex numbers and bcd coded
numbers: they have the same digits). So, to assign the value 379.25
to the
variable a of type bcd
, you can use
a.exponent = 0x4002
and
a.mantissa = 0x3792500000000000
. Standard ANSI types
float
, double
and long double
(all of
them are the same in TIGCC) are internally organized exactly at the same way
in TIGCC, except that from the aspect of the compiler, they are scalars, and
bcd
is a structure, so you cannot simply cast float
to bcd
and vice versa. For this purpose, use functions
(more precise macros) float_to_bcd,
bcd_to_float and bcd_var.
Note that due to the condition 1 <= m < 10,
the mantissa must be normalized (which means that the
first digit of the mantissa must not be zero). The consequence is that mantissa
field must always be greater or equal to 0x1000000000000000. You can construct
(artifically) structures in which this condition is not satisfied. Some functions
will work well with such (unnormalized) numbers, but many of them will not work correctly.
So, avoid creating of such illegal values: any unnormalized number may be represented
in normalized format. Anyway, don't worry about it: all numbers written using
"normal" methods are always normalized: you can create unnormalized numbers
only intentionally by direct accessing to mantissa part of a bcd
structure.
typedef float ti_float; |
An alias for the standard ANSI float type.
ti_float is an alias for the standard ANSI float type, introduced to keep backwards compatibility with antediluvian releases of TIGCC, which didn't support standard ANSI floats. See bcd for more info about the internal organization of floating point values.