﻿ F# Data Types: Real Numbers F# Data Types: Real Numbers

 Fundamentals of Real Numbers

 Introduction
 A real number is a number that displays a decimal part. This means that the number can be made of two sections separated by a symbol that is referred to as the Decimal Separator or Decimal Symbol. This symbol is different by language, country, group of languages, or group of countries. In US English, this symbol is the period as can be verified from the Regional (and Language) Settings of the Control Panel.

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.

 Floating-Point Numbers

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:

• To display the value as an exponential,
• If you want the letter e in lowercase, use %e in the placeholder. Here is an example:
```open System
open System.Windows.Forms

let exercise : Form = new Form()
exercise.Text <- "Exercise"
exercise.Width <- 180
exercise.Height <- 80
let lblNumber = new Label()

lblNumber.Left <- 12
lblNumber.Top <- 18
lblNumber.Width <- 228

let number = 4926697.54

let strNumber = sprintf "Number: %e" number

lblNumber.Text <- strNumber

Application.Run(exercise)```

This would produce: • If you want the letter e in uppercase, use %E
• If you want to display the number with 6 decimal digits, use %f
• If you want to specify the number of decimal digits, the following formula:
`%_._numberf`
Start with the % sign. It can be followed by 0 or nothing. This is followed by a period as the decimal separator. This is followed 0 or a number or 0 and a number. The number represents the number of decimal digits
• If you want the compiler to decide, based on the most compact format for e or f, use %g

To indicate the data type of the variable, you can use either the single or the float32 data type. Here is an example:

`let distance:single = 12f`

To convert a value to a 32-bit decimal number, call the float32() function.

 Double-Precision Numbers

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.

 Decimals

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`

 Operations on Decimal Numbers

 Arithmetic Operations

Real numbers support the regular arithmetic operators that are the addition, the subtraction, the multiplication, and the division.

 The Power of a Number

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.

 Built-In Functions

 Algebraic Functions

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:

• If the value of the argument is positive, the function returns 1
• If the value of the argument is negative, the function returns -1
• If the value of the argument is 0, the function returns 0

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:

• If the value of the first (left) argument is lower than the value of the second (right) argument, the function returns -1
• If the value of the first (left) argument is equal to the value of the second (right) argument, the function returns 0
• If the value of the first (left) argument is higher than that of the second (right) argument, the function returns 1

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.

 Floating-Point Functions

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.

 Trigonometric Functions

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.