Click here to Skip to main content
15,890,512 members
Articles / Programming Languages / F#

F# Primitive Types

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
23 Nov 2011CPOL2 min read 12.2K   1  
F# primitive types

A type is an abstraction and is primarily about enforcing safety. F# is statically typed, meaning that type checking is done at compile time. For example, if a function accepts an integer as a parameter, you will get a compilation error if you try to pass a non integer value.

F# contains two varieties of numeric primitives as integer and floating point numbers. All numerical primitives are listed in the attached diagram. F# uses a let binding to create a value for numeric primitives. It allows to specify values in hexadecimal (base 16), octal (base 8) or binary (base 2) with prefix 0x, 0o, 0b. Few let bindings are:

F#
> let hex =0xFCAF;;val hex : int = 64687> let bin =0b00101010y;;val bin : sbyte = 43y

Arithmetic

You can use standard arithmetic operators on numeric primitives as +, -, *, /. Sample F# code is:

F#
> 32450s + 1s;;val it : int16 = 32451s

By default, arithmetic operators do not check for overflow. So, if you exceed the range, the result will overflow to be negative.

Math

F# features all the standard math functions as:

  • abs - Absolute value of a number
  • ceil – Round up to the nearest number
  • exp – Raise a value to a power of e
  • floor – Round down to the nearest integer
  • pown – Power of an integer
  • sqrs – Square root of a given number

Conversion

F# doesn’t perform implicit conversions for primitive types. As an example, assigning int64 to int16 eliminates subtle bugs by removing surprise conversions. Developer must use an explicit conversion function like System.Convert.

Bitwise Operation

Primitive integer types support bitwise operators for manipulating values at a binary level. Bitwise operators are &&& - and, ||| - or, ^^^ - xor, <<< - left shift, >>> - right shift.

BigInt

If the application is dealing with larger data, F# provides BigInt type for representing arbitrarily long integers. BigInt uses I suffix for literals as:

F#
> open System.Numericslet megabyte = 1024I * 1024Ilet gigabyte = megabyte * 1024I

Although BigInt is heavily optimized for performance, it's much slower than using the primitive integer data types.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Ganesan Senthilvel is a passionate IT leader with demonstrated 2 decades’ experience in architecture, design and implementing cutting edge solutions to address business opportunities of Enterprise applications. He earned Masters in Computer Science and Master in Business Administration. Now, he is pursing Doctorate program in Big Data. He is consistent technical contributor via COE, Blog, Whitepaper, Summit, Certification, etc. and also highly skilled at providing coaching and mentoring to internal teams and external institutes. Earned Microsoft Certifications like MCP, MCAD and financial domain certification.
He maintains his weekly blog at http://ganesansenthilvel.blogspot.in

Comments and Discussions

 
-- There are no messages in this forum --