Home

F# Data Types: int

      

Integral Variables

 

Introduction

An integer is a natural number that includes only digits and no other symbol. The F# languages supports various types of integers. A number is said to be signed if it displays the - or the + sign to its left. This means that the number can be positive or negative. A number is said to be unsigned if it doesn't include a sign that specifies that it is positive or negative. A number that doesn't have a sign, that is, an unsigned number, is always positive.

Using an Integer

To use an integral variable, declare the variable and initialyze it with a natural number of your choice. Here is an example:

let salary = 68000

To use an integral value in a string format when calling sprintf, use either %d or %i in the placeholder. Here is an example:

open System
open System.Windows.Forms

let exercise = new Form()
exercise.Text <- "Exercise"

let lblSalary = new Label()

lblSalary.Left <- 12
lblSalary.Top <- 18
lblSalary.Width <- 328

let salary = 68000

let result = sprintf "Employee Salary: %d" salary

lblSalary.Text <- result

exercise.Controls.Add(lblSalary)

Application.Run(exercise)

To convert a value to an integer, you can use the int built-in function. Here is an example:

let hourlySalary = int "25"

Remember that, to convert an integral value to a string, call the string function. Here is an example:

open System
open System.Windows.Forms

let exercise = new Form()
exercise.Width <- 235
exercise.Height <- 108
exercise.Text <- "Salary Evaluation"

let lblHourlySalary = new Label()
lblHourlySalary.Left <- 12
lblHourlySalary.Top <- 22
lblHourlySalary.Width <- 82
lblHourlySalary.Text <- "Hourly Salary:"
exercise.Controls.Add(lblHourlySalary)

let txtHourlySalary = new TextBox()
txtHourlySalary.Left <- 95
txtHourlySalary.Top <- 18
txtHourlySalary.Width <- 40
txtHourlySalary.Text <- "0"-
exercise.Controls.Add(txtHourlySalary)

let lblYearlySalary = new Label()
lblYearlySalary.Left <- 12
lblYearlySalary.Top <- 45
lblYearlySalary.Width <- 82
lblYearlySalary.Text <- "Yearly Salary:"
exercise.Controls.Add(lblYearlySalary)

let txtYearlySalary = new TextBox()
txtYearlySalary.Left <- 95
txtYearlySalary.Top <- 45
txtYearlySalary.Width <- 110
txtYearlySalary.Text <- "0"
exercise.Controls.Add(txtYearlySalary)

let btnCalculate = new Button()
btnCalculate.Left <- 140
btnCalculate.Top <- 16
btnCalculate.Width <- 65
btnCalculate.Text <- "Calculate"

let btnCalculateClick e =
    let hourlySalary = int txtHourlySalary.Text
    let weeklySalary = hourlySalary * 40
    let monthlySalary = weeklySalary * 4
    let yearlySalary = monthlySalary * 12

    txtYearlySalary.Text <- string yearlySalary
btnCalculate.Click.Add(btnCalculateClick)
exercise.Controls.Add(btnCalculate)

Application.Run(exercise)

Here is an example of executing the program:

Integral Variables

Integral Variables

When using sprintf, if you want to use the hexadecimal version of any integer, if you want the letters of the number to display in lo wercase, use %x in the placeholder. If you want the letters to display in uppercase, use %X. Here is an example:

open System
open System.Windows.Forms

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

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

let number : int = 49298754

let strNumber = sprintf "Number: %X" number

lblNumber.Text <- strNumber

exercise.Controls.Add(lblNumber)

Application.Run(exercise)

This would produce:

Hexadecimal Number

To get the number in octal format, use %o.

A Signed Integer

You may want a variable that can hold small to (very) large negative or positive numbers, such as values between 2,147,483,648 and 2,147,484,647. Such numbers are referred to as signed integers. To indicate this when declaring a variable, specify its data type as int.

When initializing an integral variable, instead of a decimal number, you can also initialize it with a hexadecimal value whose decimal equivalent is less than 2,147,484,647. Here is an example:

let number = 0xF0488EA

When declaring the variable, to indicate that it is an integral type, you can use the int data type. Here is an example:

let salary : int = 68000

An alternative it to call a function named int32.

Short Integers

You may need a variable that can hold a value from -32768 to 32767. When declaring and initialyzing a variable for such a value, add an s on the right side of the value. Here is an example:

let schoolEffective = 1400s

To indicate that your variable will hold values between -32768 to 32767, you can specify its data type as int16. Here are two examples:

let numberOfPages : int16 = 842s
let temperature : int16 = -1544s

Because a short integer handles numbers that are larger than the signed byte, any variable you can declare for a signed byte can also be declared using int16.

To convert a value to a short integer, call the int16() function.

Long Integers

A long integer is a small to very large natural number between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. To declare a variable that can hold large values, initialize the variable with the desired value. Here is an example:

let population = 72394475

When initializing the variable, to indicate that it represents a long integer, add L to its value. Here is an example:

let population = 72394475L

To indicate the data type of a long integer variable, you can use the int64 data type. Here is an example:

let population : int64 = 72394475L;

If you want to convert a number to a long integer, use the int64() function. To convert a number to an unsigned long integer, call the uint64() function.

Native Integers

A native integer is F#'s own interpretation of natural numbers. To support this, the language has a type named nativeint. To apply that type to a value, when initialydeclaring a variable that will hold values as a native integer. When initializing the variable, add an n suffix to its value.

let number : nativeint = 16n;

To use its value in sprintf, use %d. To convert a value to native integer, call the nativeint() function.

To convert the number an unsigned native integer, call the unativeint() function.

Unsigned Native Integers

An unsigned native integer is a positive F# integer. To declare a variable for an unsigned native integer, when initializing the variable, add a un suffix to its value. To include the value in sprintf, use %d.

Operations on Integers

   

Unary Operations

To indicate that a number or a variable is positive, either don't add any operator to it or add + to its left.

To indicate that a number is negative, add a - sign to its left.

Infinity is the highest number that exists. To support it, the F# language provides the infinity keyword. Here is an example of accessing it:

let population = infinity

Arithmetic Operations

Integral numbers support all the regular arithmetic operators learned in elementary school:

  • The + sign is used to add two or more numbers
  • The - symbol is used to subtract one number from another
  • The * operator is used to multiply two or more numbers
  • The division represented by / is used to divide a first number, called a numerator, by another number, called the denominator. The formula to use is
    numerator / denominator
    The denominator must not be equal to 0
  • The % operator is used to find the remainder when a numerator is divided by a denominator

Byte-Shift Operations

The F# language supports operations that consist of moving the values, namely 0 or 1, stored in the bits of the computer memory:

  • The <<< operator is used to shift a certain number of bytes from a number (actually the memory area where a number is stored) to its left in the computer memory. Here is an example:
    let a = 248
    let b = 5
    let leftShift = a <<< b
    This would produce 7936
  • The >>> operator is used to shift a certain number of bytes from a number to its right

Bitwise Operations

A bitwise operation consists of adding or removing (actually replacing or switching) the 1 and 0 values stored in the computer memory:

  • To add two bits, use the &&& operator. This is called the bitwise-AND operator. Here is an example of applying this operator:
    let a = 248
    let b = 125
    let bitwise = a &&& b
    This would produce 120
  • The bitwise OR operation consists of reversing the value of a bit. During this operator, if the value of a bit is 1, it is changed to 0, and vice-versa. To perform this operation, use the ||| operator. Here is an example:
    let a = 248
    let b = 125
    let bitwise = a ||| b
    This would produce: 253
 
   
     
 

Home Copyright © 2009-2015 FunctionX Home