Integer Arithmetic

Category:
Arithmetic
Description: This sample shows some basic integer arithmetic
Code:
let SampleArithmetic1() =
let x = 10 + 12 - 3
let y = x * 2 + 1
let r1,r2 = x/3, x%3
printfn "x = %d, y = %d, r1 = %d, r2 = %d" x y r1 r2


Execution Result:
x = 19, y = 39, r1 = 6, r2 = 1

Floating Point Arithmetic

Category:
Arithmetic
Description: This sample shows some basic floating point arithmetic
Code:
let SampleArithmetic2() =
let x = 10.0 + 12.0 - 3.0
let y = x * 2.0 + 1.0
let r1 = x/3.0
printfn "x = %g, y = %g, r1 = %g" x y r1


Execution Result:
x = 19, y = 39, r1 = 6.33333

Converting between numeric types

Category:
Arithmetic
Description: This sample shows how to convert between various numeric types
Code:
let SampleArithmetic3() =
// Manipulating double-precision (64-bit) floating point numbers
let pi1 = float 3 + 0.1415 // 'float' is an overloaded conversion operator
let pi2 = double 3 + 0.1415 // identical - 'double' is a synonym for 'float'
printfn "pi1 = %f, pi2 = %f" pi1 pi2

let i1 = int 3.1415
let i2 = int64 3.1415
printfn "i1 = %d, i2 = %d" i1 i2

// Manipulating single-precision (32-bit) floating point numbers
let f32a = 2.1415f + 1.0f // float32 (System.Single)
let f32b = 2.1415f + float32 1 // float32 - identical
printfn "f32a = %f, f32b = %G" f32a f32b

// Manipulating bytes
let byteA = byte (3+4) // byte
let byteB = 255uy // byte
let byteC = 0xFFuy // byte
let byteD = byte 0xFF // byte
printfn "byteA = %d, byteB = %d" byteA byteB

Execution Result:
pi1 = 3.141500, pi2 = 3.141500
i1 = 3, i2 = 3
f32a = 3.141500, f32b = 3.1415
byteA = 7, byteB = 255

Basic type and printf

Category:
Arithmetic
Description: Basic type and printf
Code:
let TypeAndPrintf() = 
let int = 42
let string = "This is a string"
let char = 'c'
let bool = true
let bytearray = "This is a byte string"B
let hexint = 0x34
let octalint = 0o42
let binaryinteger = 0b101010
let signedbyte = 68y
let unsignedbyte = 102uy
let smallint = 16s
let smalluint = 16us
let integer = 345l
let usignedint = 345ul
let nativeint = 765n
let unsignednativeint = 765un
let long = 12345678912345789L
let unsignedlong = 12345678912345UL
let float32 = 42.8F
let float = 42.8

printfn "int = %d or %A" int int
printfn "string = %s or %A" string string
printfn "char = %c or %A" char char
printfn "bool = %b or %A" bool bool
printfn "bytearray = %A" bytearray
printfn "hex int = %x or %A" hexint hexint
printfn "HEX INT = %X or %A" hexint hexint
printfn "oct int = %o or %A" octalint octalint
printfn "bin int = %d or %A" binaryinteger binaryinteger
printfn "signed byte = %A" signedbyte
printfn "unsigned byte = %A" unsignedbyte
printfn "small int = %A" smallint
printfn "small uint = %A" smalluint
printfn "int = %i or %A" integer integer
printfn "uint = %i or %A" usignedint usignedint
printfn "native int = %A" nativeint
printfn "unsigned native int = %A" unsignednativeint
printfn "long = %d or %A" long long
printfn "unsigned long = %A" unsignedlong
printfn "float = %f or %A" float32 float32
printfn "double = %f or %A" float float
//--------------------------------------------------------

Execution Result:
int = 42 or 42
string = This is a string or "This is a string"
char = c or 'c'
bool = true or true
bytearray = [|84uy; 104uy; 105uy; 115uy; 32uy; 105uy; 115uy; 32uy; 97uy; 32uy; 98uy; 121uy;
116uy; 101uy; 32uy; 115uy; 116uy; 114uy; 105uy; 110uy; 103uy|]
hex int = 34 or 52
HEX INT = 34 or 52
oct int = 42 or 34
bin int = 42 or 42
signed byte = 68y
unsigned byte = 102uy
small int = 16s
small uint = 16us
int = 345 or 345
uint = 345 or 345u
native int = 765n
unsigned native int = 765un
long = 12345678912345789 or 12345678912345789L
unsigned long = 12345678912345UL
float = 42.800000 or 42.7999992f
double = 42.800000 or 42.8

Bitwise Integer Operations

Category:
Arithmetic
Description: This sample shows how to use the bitwise integer operations
Code:
let Sample4() =
// Operators over integers:
let x1 = 0xAB7F3456 &&& 0xFFFF0000
let x2 = 0xAB7F3456 ||| 0xFFFF0000
let x3 = 0x12343456 ^^^ 0xFFFF0000
let x4 = 0x1234ABCD <<< 1
let x5 = 0x1234ABCD >>> 16

// Also over other integral types, e.g. Int64:
let x6 = 0x0A0A0A0A012343456L &&& 0x00000000FFFF0000L

// Also over other integral types, e.g. unsigned Int64:
let x6u = 0x0A0A0A0A012343456UL &&& 0x0000FFFF00000000UL

// And bytes:
let x7 = 0x13uy &&& 0x11uy

// Now print the results:
printfn "x1 = 0x%08x" x1;
printfn "x2 = 0x%08x" x2;
printfn "x3 = 0x%08x" x3;
printfn "x4 = 0x%08x" x4;
printfn "x5 = 0x%08x" x5;
printfn "x6 = 0x%016x" x6;
printfn "x6u = 0x%016x" x6u;
printfn "x7 = 0x%02x" (int x7)



Execution Result:
x1 = 0xab7f0000
x2 = 0xffff3456
x3 = 0xedcb3456
x4 = 0x2469579a
x5 = 0x00001234
x6 = 0x0000000012340000
x6u = 0x0000a0a000000000
x7 = 0x11

Last edited Sep 12, 2011 at 11:13 PM by ttliu2000, version 2

Comments

No comments yet.