F# – Data Types

  • Post author:
  • Post category:F#
  • Post comments:1 Comment
F# - Data Types

The F# data types can are classified as follows −

  • Integral types
  • Floating point types
  • Text types
  • Other types

Integral F# Data Type

The following table provides the integral data types of F#. These are basically integer data types.

F# TypeSizeRangeExampleRemarks
sbyte1 byte-128 to 12742y-11y8-bit signed integer
byte1 byte0 to 25542uy200uy8-bit unsigned integer
int162 bytes-32768 to 3276742s-11s16-bit signed integer
uint162 bytes0 to 65,53542us200us16-bit unsigned integer
int/int324 bytes-2,147,483,648 to 2,147,483,64742-1132-bit signed integer
uint324 bytes0 to 4,294,967,29542u200u32-bit unsigned integer
int648 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,80742L-11L64-bit signed integer
uint648 bytes0 to 18,446,744,073,709,551,61542UL200UL64-bit unsigned integer
bigintAt least 4 bytesany integer42I14999999999999999999999999999999Iarbitrary precision integer

Example

(* single byte integer *)
let x = 268.97f
let y = 312.58f
let z = x + y

printfn "x: %f" x
printfn "y: %f" y
printfn "z: %f" z

(* unsigned 8-bit natural number *)

let p = 2uy
let q = 4uy
let r = p + q

printfn "p: %i" p
printfn "q: %i" q
printfn "r: %i" r

(* signed 16-bit integer *)

let a = 12s
let b = 24s
let c = a + b

printfn "a: %i" a
printfn "b: %i" b
printfn "c: %i" c

(* signed 32-bit integer *)

let d = 212l
let e = 504l
let f = d + e

printfn "d: %i" d
printfn "e: %i" e
printfn "f: %i" f

When you compile and execute the program, it yields the following output −

x: 1
y: 2
z: 3
p: 2
q: 4
r: 6
a: 12
b: 24
c: 36
d: 212
e: 504
f: 716

Floating Point Data Types

The following table provides the floating-point data types of F#.

F# TypeSizeRangeExampleRemarks
float324 bytes±1.5e-45 to ±3.4e3842.0F-11.0F32-bit signed floating-point number (7 significant digits)
float8 bytes±5.0e-324 to ±1.7e30842.0-11.064-bit signed floating-point number (15-16 significant digits)
decimal16 bytes±1.0e-28 to ±7.9e2842.0M-11.0M128-bit signed floating-point number (28-29 significant digits)
BigRationalAt least 4 bytesAny rational number.42N-11NArbitrary precision rational number. Using this type requires a reference to FSharp.PowerPack.dll.

Example

(* 32-bit signed floating point number *)
(* 7 significant digits *)

let d = 212.098f
let e = 504.768f
let f = d + e

printfn "d: %f" d
printfn "e: %f" e
printfn "f: %f" f

(* 64-bit signed floating point number *)
(* 15-16 significant digits *)
let x = 21290.098
let y = 50446.768
let z = x + y

printfn "x: %g" x
printfn "y: %g" y
printfn "z: %g" z

When you compile and execute the program, it yields the following output −

d: 212.098000
e: 504.768000
f: 716.866000
x: 21290.1
y: 50446.8
z: 71736.9

Text Data Types

The following table provides the text data types of F#.

F# TypeSizeRangeExampleRemarks
char2 bytesU+0000 to U+ffff‘x”\t’Single Unicode characters
string20 + (2 * string’s length) bytes0 to about 2 billion characters“Hello””World”Unicode text

Example

let choice = 'y'
let name = "Zara Ali"
let org = "Tutorials Point"

printfn "Choice: %c" choice
printfn "Name: %s" name
printfn "Organisation: %s" org

When you compile and execute the program, it yields the following output −

Choice: y
Name: Zara Ali
Organisation: Tutorials Point

Other Data Types

The following table provides some other data types of F#.

F# TypeSizeRangeExampleRemarks
bool1 byteOnly two possible values, true or falsetrue-falseStores boolean values

Example

let trueVal = true
let falseVal = false

printfn "True Value: %b" (trueVal)
printfn "False Value: %b" (falseVal)

When you compile and execute the program, it yields the following output −

True Value: true
False Value: false

Next Topic – Click Here

This Post Has One Comment

Leave a Reply