In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use the F# operator to solve overflow anomalies and achieve efficient arithmetic operations. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Don't say much. Let's take a look at it with the editor.
The source of the high efficiency and high yield of F# is that it is based on the tried and tested concept of functional programming.
Use F# for arithmetic operations
Basic types:
Types
Description
Example
.net type
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#, addition, subtraction, multiplication and division of numbers are not checked (unchecked); that is, if you go out of range, you will not get an exception. For example, 2147483647 is a 32-bit integer of *:
> 21474836474th; val it: int =-2147483648
At the same time, we also provide an implementation to check for overflows: Microsoft.FSharp.Core.Operators.Checked. The operation implemented in this module (module) will throw a System.OverflowException exception when the removal occurs.
If you want to avoid overflow, you can use the decimal,bigint and bignum types.
Dividing by zero will result in System.DivideByZeroException, except for floating-point numbers (floating-point number), which will return Infinity and-Infinity.
Operator overloading is determined by type derivation (type inference)-if there is no overloading, the F# convention uses the operator of a 32-bit integer.
If you want to use operators of the specified type, you must use type annotations (type annotation) to help the type deducer derive the correct results:
> let squareAndAdd a b = a * a + b *; val squareAndAdd: int-> int-> int
If we need to specify an operator that uses float, just:
> let squareAndAdd (a:float) b = a * a + b; val squareAndAdd: float-> float-> float
This is what the type pusher does.
Bit (bitwise) operation
Operator
Description
Give an example
Result
& & &
Vs.
0x65 & 0x0F
0x05
|
Or
0x65 | 0x18
0x7D
Thank you for your help.
XOR
0x65 thanks to 0x0F
0x6A
~ ~
Seek the reverse
~ 0x65
0xFFFFFF9a
> 3
0x0C
Encodes a 32-bit integer into (encode) 1, or 5 bytes, and returns it with a list of numbers.
Let encode (n: int32) = if (n > = 0 & & n = 0x80 & & n > 8)) & 0xFF; (n & 0xFF)] else [0xC0; (n > 24) & 0xFF); ((n > 16) & & 0xFF) ((n > 8) & 0xFF); (n & 0xFF)]
Call:
> 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]
Digital type conversion
There is no implicit conversion between different numeric types. Explicit type conversions must be performed using the appropriate operators:
Operator
Description
Usage
Result
Sbyte
Convert to sbyte
Sbyte (- 17)
-17y
Byte
Convert to byte
Byte 255
255uy
Int16
Convert to int16
Int16 0
0s
Uint16
Convert to uint16
Uint16 65535
65535us
Int/int32
Convert to int
Int 17.8
seventeen
Uint32
Convert to uint32
Uint32 12
12u
Int64
Convert to int64
Int64 (- 100.4)
-100L
Uint64
Convert to uint64
Uint64 1
1UL
Float32
Convert to float32
Float32 65
65.0f
Float
Convert to float
Float 65
65.0
It is important to note that these transformations do not check for overflows. No exception is thrown. If you need to use overflow exceptions, you still need to use the operators under the Microsoft.FSharp.Core.Operators.Checked module. Alternatively, you can use .NET 's System.Convert. However, using System.Convert brings some problems, and you need to use type annotations to help the type pusher work.
Digital comparison
The operators that can be used are =, min, and max. It all has the same literal meaning.
It is important to note that when operating on floating-point numbers, these operators implement the NaN of IEEE. Any comparison operation that contains NaN returns false.
The above is how to use the F# operator to solve overflow exceptions and achieve efficient arithmetic operations. Xiaobian believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.