【F#2.0系列】使用F#进行算术操作


使用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);
                  ((n >>> 16) &&& 0xFF);
                  ((n >>> 8)  &&& 0xFF);
                   (n         &&& 0xFF) ]

  调用:

> 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]

  数字类型转换

  不同数字类型之间不会隐式转换。必须使用相应的操作符进行显式的类型转换:

  操作符   描述   用法   结果
  sbyte   转换为sbyte   sbyte (-17)   -17y
  byte   转换为byte   byte 255   255uy
  int16   转换为int16   int16 0   0s
  uint16   转换为uint16   uint16 65535   65535us
  int/int32   转换为int   int 17.8   17
  uint32   转换为uint32   uint32 12   12u
  int64   转换为int64   int64 (-100.4)   -100L
  uint64   转换为uint64   uint64 1   1UL
  float32   转换为float32   float32 65   65.0f
  float   转换为float   float 65   65.0

  需要注意的是,这些转换都是不检查溢出的。不会抛出异常。如需要使用溢出异常,还是需要使用Microsoft.FSharp.Core.Operators.Checked模块下的操作符。或者也可以使用.NET的System.Convert。但使用System.Convert会带来一些问题,需要使用类型注释来帮助类型推导器工作。

  数字比较

  可以使用的操作符为=,<>,<,<=,>,>=,min和max。全都和字面的意义相同。

  需要注意的是,当对浮点数进行操作的时候,这些操作符实现了IEEE的NaN。任何包含NaN的比较操作都会返回false。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3