FutureBasic Logo

<<    Index    >> FutureBasic

Appendix R - Rounding Options   appendix



Other Rounding Functions
In addition to the FB keywords( such as int, fix, frac ) involved in rounding, C offers many rounding functions and options via its math library ( math.h ). These functions are available via FB's math.incl header and the FB Editor recognizes them. Functions such as fn ceil, fn floor and fn trunc ( and many others ) are available.

An optional header1., fenv.incl provides functions allowing the programmer to fine-tune the app's specific rounding needs. These rounding options are available to the math.incl functions but in the example below they are used with FB's int function. This works because FB internally calls the rint() function from math.incl when the int function is used. The example's print command comments show the expected output.

Example of Rounding Options & one math.incl Function

include "fenv.incl" // "fenv.incl" is an optional header1., so programmer must include it for access to fn fesetround

print : print @"FE_TO_NEAREST (default)"
print int(13.5) // 14
print int(14.5) // 14

fn fesetround( FE_UPWARD ) // fn fesetround's constant stored in "fenv.incl"
print : print @"FE_UPWARD"
print int(13.5) // 14
print int(14.5) // 15

fn fesetround( FE_DOWNWARD )
print : print @"FE_DOWNWARD"
print int(13.5) // 13
print int(14.5) // 14

fn fesetround( FE_TOWARDZERO )
print : print @"FE_TOWARDZERO"
print int(13.5) // 13
print int(-13.5) // -13

print : print @"Truncating the decimal digits with the trunc function from math.incl"
print fn trunc(34.52345) // 34

HandleEvents

Footnote
1. "Optional Header" refers to a file the programmer must include ( via the include keyword ) in order to access its contents. FB automatically includes many headers but optional headers require this extra step from the programmer.