## `frac` -- the fractional part of a number

#### Introduction

`frac``(x)` represents the ``fractional part'' `x-floor(x)` of the number `x`.

#### Call(s)

`frac(x)`

#### Parameters

`x` | - | an arithmetical expression

#### Returns

an arithmetical expression.

### Overloadable:

`x`

#### Side Effects

The function is sensitive to the environment variable `DIGITS` which determines the numerical working precision.

#### Related Functions

`floor`

#### Details

- For complex arguments, `frac` is applied separately to the real and imaginary part.
- For real numbers, the value `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.
- For integer arguments, `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.

#### Example 1

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))

#### Example 2

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

#### Example 3

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:

#### Changes

- no changes

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000