The f#2.0 series uses F # for arithmetic operations

Source: Internet
Author: User
Tags arithmetic

Using F # for arithmetic operations

Basic type:

Type Describe Example . NET types
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

In F #, subtraction operations on numbers are not checked (unchecked), which means that if they are out of range, they will not get an exception. For example, 2147483647 is the largest 32-bit integer:

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

At the same time, we also provide a check overflow implementation: Microsoft.FSharp.Core.Operators.Checked. The operation implemented in this module (module) throws a System.OverflowException exception when the removal occurs.

If you want to avoid overflow, you can use the Decimal,bigint and Bignum types.

Except 0 will get system.dividebyzeroexception, except for floating-point numbers (floating-point number), the floating-point number 0 will return Infinity and-infinity.

Operator overloading is determined by type inference-if there is no overload, the F # Convention uses an operator of 32-bit integers.

If you want to use an operator of the specified type, you must use the type annotation (type annotation) to help the type derivation to derive the correct result:

> let squareAndAdd a b = a * a + b;;
val squareAndAdd : int -> int -> int

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.