On both sides of the Decimal Symbol, digits are used to specify the value of the number. The number of digits on the right side of the symbol determines how much precision the number offers.
F# supports floating-point numbers that range from ±1.5 — 10−45 to ±3.4 — 1038 with a precision of 7 digits that can fit in 32 bits. To declare such a variable, add f or F to its value. Here is an example:
let distance = 12f
To include a floating-point number in sprintf, you have various options:
let distance:single = 12f
To convert a value to a 32-bit decimal number, call the float32() function.
When a variable is larger than the single can handle and requires more precision, you should declare it using either the float or the double keyword. Here is an example:
let number = 62834.9023
A variable declared as double uses 64 bits to store very large numbers ranging from ±5.0 x 10−324 to ±1.7 x —10308 with a precision of 15 or 16 digits.
To convert a value to a 64-bit decimal number, call the float() function.
The decimal data type can be used to declare a variable that would hold significantly large values that can be stored in a combination of 128 bits. You declare such a variable using the decimal keyword. The values stored in a decimal variable can range from ±1.0 — 10−28 to ±7.9 — 1028 with a precision of 28 to 29 digits.
After declaring a decimal variable, you can initialize it with a natural number. To indicate that the variable holds a decimal value, when initializing it, add an M suffix to its value. Here is an example:
let hourlySalary: decimal = 24.25M
As seen in previous sections and this one, when declaring and initializing a real variable, the suffix you give to its assigned value indicates to the compiler the actual type of value and the type of memory that would be allocated for the variable.
To display the decimal value in sprintf, user %M in the placeholder.
To convert a number to decimal, call the decimal() function. Here is an example:
let result = decimal 513
Real numbers support the regular arithmetic operators that are the addition, the subtraction, the multiplication, and the division.
Decimal numbers support the operation that consists of raising one number to the power of another number. This is done using the ** operator. Here is an example:
let a = 8.25 let b = 5.14 let power = a ** b
This operation is not supported on natural numbers. If you want to perform it on integers, simply add .00 to each number or convert each number to float.
The F# language provides a series of functions you can use in your programs.
The numbers in your program can be positive or negative. A negative number is preceded by a - sign. To find the sign of a number, call a function named sign. This function takes one argument and works as follows:
To find the absolute value of a number or a variable, use the abs() function.
The compare() function is used to compare two numeric values. The function takes two arguments and behaves as follows:
To calculate the natural logarithm of a floating-point number, call a function named log.
To calculate the logarithm to base 10 of a decimal number, call the log10() function.
To round a number, call a function named round.
To truncate a number, call a function named truncate.
To find the ceiling of a number, call the ceil() function. To get the floor of a number, call the floor() function.
To calculat the power of a number raised to a power, use a function named pown. This function takes two arguments.
To calculate the exponential of a function, call a function named exp.
To calculate the square root of a number, call the sqrt() function.
If you have a decimal number and you want to find its sine, the F# language provides a function named sin. Here is an example of calling it:
let result = sin 228.82 sprintf "The sine of 128 is %.3f" result
This would produce:
The sine of 128 is 0.494
To get the inverse sine of a function, call the asin() function. If you want to calculate the hyperbolic sine of a number, call the sinh() function.
To let you calculate the cosine of a decimal number, the F# language is equipped with a function named cos.
To get the inverse cosine of a number, call the acos() function. To get the hyperbolic cosine of a decimal number, use the cosh() function.
To calculate the tangent of a number, call a function named tan. To get the arctangent of a number, call the atan() function. To find the hyperbolic tangent of a number, call the tanh() function.
On the other hand, if you have a fraction represented as x / y, to get its inverse tangent, call the atan2() function. This function takes two arguments as the denominator followed by the numerator.