NEXT ·  UP ·  PREVIOUS ·  CONTENTS ·  INDEX

## The Real structure

It would not be very misleading to say that the Real structure contains all of the corresponding real equivalents of the integer functions from the Int structure. In addition to these, it provides conversions to and from integers. For the former we have Real.trunc, which truncates; Real.round, which rounds up; Real.floor which returns the greatest integer less than its argument; and Real.ceil, which returns the least integer greater than its argument. For the latter we have Real.fromInt, which we have already seen. One function which is necessarily a little more complicated than the integer equivalent is the Real.fmt function which allows the programmer to select between scientific and fixed-point notation or to allow the more appropriate format to be used for the value being formatted. In addition to this a number may be specified; it will default to six for scientific or fixed-point notation and twelve otherwise. The fine distinction between the import of the numbers is that it specifies decimal places for scientific and fixed-point notation and significant digits otherwise. Functions cannot vary the number of arguments which they take so once again the option type is used to signify the number of required digits. Here are some examples of the use of this function. Note that the parentheses are needed in all cases.

 Real.fmt (StringCvt.FIX NONE) 3.1415 = "3.141500" Real.fmt (StringCvt.SCI NONE) 3.1415 = "3.141500E00" Real.fmt (StringCvt.GEN NONE) 3.1415 = "3.1415" Real.fmt (StringCvt.FIX (SOME 3)) 3.1415 = "3.142" Real.fmt (StringCvt.SCI (SOME 3)) 3.1415 = "3.142E00" Real.fmt (StringCvt.GEN (SOME 3)) 3.1415 = "3.14"

NEXT ·  UP ·  PREVIOUS ·  CONTENTS ·  INDEX