用好F#操作符解決溢出異常 實(shí)現(xiàn)高效算術(shù)操作
F#高效高產(chǎn)的源頭就在于其構(gòu)建在久經(jīng)考驗(yàn)的函數(shù)式編程理念之上。
使用F#進(jìn)行算術(shù)操作
基本類型:
類型
|
描述
|
示例
|
.NET 類型
|
bool
|
True/false values
|
true,false
|
System.Boolean
|
byte
|
8-bit unsigned integers
|
0uy,19uy,0xFFuy
|
System.Byte
|
sbyte
|
8-bit signed integers
|
0y, 19y,0xFFy
|
System.SByte
|
int16
|
16-bit signed integers
|
0s, 19s,0x0800s
|
System.Int16
|
uint16
|
16-bit unsigned integers
|
0us,19us,0x0800us
|
System.UInt16
|
int, int32
|
32-bit signed integers
|
0, 19,0x0800,0b0001
|
System.Int32
|
uint32
|
32-bit unsigned integers
|
0u, 19u,0x0800u
|
System.UInt32
|
int64
|
64-bit signed integers
|
0L, 19L,0x0800L
|
System.Int64
|
uint64
|
64-bit unsigned integers
|
0UL,19UL,0x0800UL
|
System.UInt64
|
nativeint
|
Machine-sized signed integers
|
0n, 19n,0x0800n
|
System.IntPtr
|
unativeint
|
Machine-sized unsigned integers
|
0un,19un,0x0800un
|
System.UIntPtr
|
single,float32
|
32-bit IEEE floating-point
|
0.0f,19.7f,1.3e4f
|
System.Single
|
double,float
|
64-bit IEEE floating-point
|
0.0,19.7,1.3e4
|
System.Double
|
decimal
|
High-precision decimal values
|
0M, 19M,19.03M
|
System.Decimal
|
bigint
|
Arbitrarily large integers
|
0I, 19I
|
Math.BigInt
|
bignum
|
Arbitrary-precision rationals
|
0N, 19N
|
Math.BigNum
|
unit
|
The type with only one value
|
()
|
Core.Unit
|
在F#中,對(duì)數(shù)字的加減乘除操作均是不檢查的(unchecked);就是說(shuō)如果超出范圍,不會(huì)得到異常。例如,2147483647是***的32位整數(shù):
- > 2147483647+1;;
- val it : int = -2147483648
同時(shí),我們也提供了檢查溢出的實(shí)現(xiàn):Microsoft.FSharp.Core.Operators.Checked。這個(gè)模塊(module)中實(shí)現(xiàn)的操作將在移除發(fā)生時(shí)拋出System.OverflowException異常。
如果希望避免溢出,可以使用decimal,bigint和bignum類型。
除零將會(huì)得到System.DivideByZeroException,但浮點(diǎn)數(shù)(floating-point number)除外,浮點(diǎn)數(shù)除零將會(huì)返回Infinity和-Infinity。
通過(guò)類型推導(dǎo)(type inference)來(lái)確定操作符重載—如果沒(méi)有重載則F#約定使用32位整數(shù)的操作符。
如果希望使用指定類型的操作符,則必須使用類型注釋(type annotation)來(lái)幫助類型推導(dǎo)器推導(dǎo)出正確的結(jié)果:
- > let squareAndAdd a b = a * a + b;;
- val squareAndAdd : int -> int -> int
如果我們需要指定使用float的操作符,只需:
- > let squareAndAdd (a:float) b = a * a + b;;
- val squareAndAdd : float -> float -> float
這就是類型推導(dǎo)器發(fā)揮的作用。
位(bitwise)操作
操作符 |
描述 |
舉例 |
結(jié)果 |
&&& |
與 |
0x65 &&& 0x0F |
0x05 |
||| |
或 |
0x65 ||| 0x18 |
0x7D |
ˆˆˆ |
異或 |
0x65ˆˆˆ0x0F |
0x6A |
~~~ |
求反 |
~~~0x65 |
0xFFFFFF9a |
<<< |
左移 |
0x01 <<< 3 |
0x08 |
>>> |
右移 |
0x65 >>> 3 |
0x0C |
將一個(gè)32位整數(shù)編碼成(encode) 1,2,或5個(gè)字節(jié),并用一個(gè)數(shù)字列表返回。
- let encode (n: int32) =
- if (n >= 0 && n <= 0x7F) then [ n ]
- elif (n >= 0x80 && n <= 0x3FFF) then [ (0x80 ||| (n >>> 8)) &&& 0xFF;
- (n &&& 0xFF) ]
- else [ 0xC0; ((n >>> 24) &&& 0xFF);
- ((n >>> 16) &&& 0xFF);
- ((n >>> 8) &&& 0xFF);
- (n &&& 0xFF) ]
調(diào)用:
- > encode 32;;
- val it : int32 list = [32]
- > encode 320;;
- val it : int32 list = [129; 64]
- > encode 32000;;
- val it : int32 list = [192; 0; 0; 125; 0]
數(shù)字類型轉(zhuǎn)換
不同數(shù)字類型之間不會(huì)隱式轉(zhuǎn)換。必須使用相應(yīng)的操作符進(jìn)行顯式的類型轉(zhuǎn)換:
操作符
|
描述
|
用法
|
結(jié)果
|
sbyte
|
轉(zhuǎn)換為sbyte
|
sbyte (-17)
|
-17y
|
byte
|
轉(zhuǎn)換為byte
|
byte 255
|
255uy
|
int16
|
轉(zhuǎn)換為int16
|
int16 0
|
0s
|
uint16
|
轉(zhuǎn)換為uint16
|
uint16 65535
|
65535us
|
int/int32
|
轉(zhuǎn)換為int
|
int 17.8
|
17
|
uint32
|
轉(zhuǎn)換為uint32
|
uint32 12
|
12u
|
int64
|
轉(zhuǎn)換為int64
|
int64 (-100.4)
|
-100L
|
uint64
|
轉(zhuǎn)換為uint64
|
uint64 1
|
1UL
|
float32
|
轉(zhuǎn)換為float32
|
float32 65
|
65.0f
|
float
|
轉(zhuǎn)換為float
|
float 65
|
65.0
|
需要注意的是,這些轉(zhuǎn)換都是不檢查溢出的。不會(huì)拋出異常。如需要使用溢出異常,還是需要使用Microsoft.FSharp.Core.Operators.Checked模塊下的操作符?;蛘咭部梢允褂?NET的System.Convert。但使用System.Convert會(huì)帶來(lái)一些問(wèn)題,需要使用類型注釋來(lái)幫助類型推導(dǎo)器工作。
數(shù)字比較
可以使用的操作符為=,<>,<,<=,>,>=,min和max。全都和字面的意義相同。
需要注意的是,當(dāng)對(duì)浮點(diǎn)數(shù)進(jìn)行操作的時(shí)候,這些操作符實(shí)現(xiàn)了IEEE的NaN。任何包含NaN的比較操作都會(huì)返回false。
原文標(biāo)題:【F#2.0系列】使用F#進(jìn)行算術(shù)操作
鏈接:http://www.cnblogs.com/pandora/archive/2010/08/26/FSharp_Using_Number.html
【編輯推薦】