frac
-- the fractional part of a
numberfrac
(x)
represents the ``fractional part''
x-floor(x)
of the number x
.
frac(x)
x |
- | an arithmetical expression |
an arithmetical expression.
x
The function is sensitive to the environment variable DIGITS
which determines the numerical
working precision.
frac
is applied separately to
the real and imaginary part.x-floor(x)
represented by
frac
(x)
is a number from the interval
[0, 1). For positive arguments, you may think of
frac
as truncating all digits before the decimal
point.0
is returned. For rational
arguments, a rational number is returned. For arguments that contain
symbolic identifiers, symbolic function calls are returned. For
floating point arguments or non-rational exact expressions, floating
point values are returned.If the argument is a floating point number of absolute value larger than 10^DIGITS, then the result is affected by internal non-significant digits! Cf. example 2.
Exact numerical data that are neither integers nor
rational numbers are approximated by floating point numbers. For such
arguments, the result depends on the present value of
DIGITS
! Cf. example 3.
We demonstrate the fractional part of real and complex numbers:
>> frac(1234), frac(123/4), frac(1.234)
0, 3/4, 0.234
>> frac(-1234), frac(-123/4), frac(-1.234)
0, 1/4, 0.766
>> frac(3/2 + 7/4*I), frac(4/3 + 1.234*I)
1/2 + 3/4 I, 0.3333333333 + 0.234 I
The fractional part of a symbolic numerical expression is returned as a floating point value:
>> frac(exp(123)), frac(3/4*sin(1) + I*tan(3))
0.7502040792, 0.6311032386 + 0.8574534569 I
Expressions with symbolic identifiers produce symbolic function calls:
>> frac(x), frac(sin(1) + x^2), frac(exp(-x))
2 frac(x), frac(sin(1) + x ), frac(exp(-x))
Care should be taken when computing the fractional part of floating point numbers of large absolute value:
>> 10^13/3.0
3.333333333e12
Note that only the first 10 decimal digits are ``significant''. Further digits are subject to round-off effects caused by the internal binary representation. These ``insignificant'' digits can enter the fractional part:
>> frac(10^13/3.0)
0.3333332539
The mantissa of the next floating point number does not have enough digits to store ``digits after the decimal point'':
>> floor(10^25/9.0), ceil(10^25/9.0), frac(10^25/9.0)
1111111111111111111081984, 1111111111111111111081984, 0.0
Exact numerical expressions are converted to floating
point numbers. Consequently, the present setting of DIGITS
affects the result:
>> x := 10^30 - exp(30)^ln(10) + 1/3
ln(10) 3000000000000000000000000000001/3 - exp(30)
Note that the exact value of this number is 1/3. Floating point evaluation can be subject to severe cancellation:
>> DIGITS := 20: frac(x)
0.0
The floating point result is more accurate when a higher precision used:
>> DIGITS := 30: frac(x)
0.333334211260080337524414062
>> delete x, DIGITS: