日期:2010-01-29  浏览次数:20378 次

使用F#进行算术操作

  基本类型:

  类型   描述   示例   .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#中,对数字的加减乘除操作均是不检查的(unchecked);就是说如果超出范围,不会得到异常。例如,2147483647是最大的32位整数:

> 2147483647+1;;
val it : int = -2147483648

  同时,我们也提供了检查溢出的实现:Microsoft.FSharp.Core.Operators.Checked。这个模块(module)中实现的操作将在移除发生时抛出System.OverflowException异常。

  如果希望避免溢出,可以使用decimal,bigint和bignum类型。

  除零将会得到System.DivideByZeroException,但浮点数(floating-point number)除外,浮点数除零将会返回Infinity和-Infinity。

  通过类型推导(type inference)来确定操作符重载—如果没有重载则F#约定使用32位整数的操作符。

  如果希望使用指定类型的操作符,则必须使用类型注释(type annotation)来帮助类型推导器推导出正确的结果:

> 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

  这就是类型推导器发挥的作用。

  位(bitwise)操作

  位操作符:

  操作符   描述   举例   结果
  &&&   与   0x65 &&& 0x0F   0x05
     或   0x65 0x18   0x7D
  ???   异或   0x65???0x0F   0x6A
  ~~~   求反   ~~~0x65   0xFFFFFF9a
  <<<    左移   0x01 <<< 3   0x08
  >>>    右移   0x65 >>> 3   0x0C

  将一个32位整数编码成(encode) 1,2,或5个字节,并用一个数字列表返回。

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);