Numbers
Numbers in Harneet include integers and floating-point values with comprehensive instance methods for common mathematical operations.
Overview
Harneet provides: - Integers - whole numbers with various sizes (int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr) - Floats - decimal numbers (float32, float64) - Method-rich - built-in .abs(), .min(), .max(), .clamp(), .toString() for integers; additional .round(), .floor(), .ceil(), .isNaN(), .isInf() for floats - Type-safe - strict type checking with explicit conversions
Integer Types
Fixed-size Signed Integers
int- Platform-dependent (32 or 64-bit)int8- 8-bit signed (-128 to 127)int16- 16-bit signed (-32,768 to 32,767)int32- 32-bit signed (-2,147,483,648 to 2,147,483,647)int64- 64-bit signed (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
Fixed-size Unsigned Integers
uint- Platform-dependent (32 or 64-bit)uint8- 8-bit unsigned (0 to 255)uint16- 16-bit unsigned (0 to 65,535)uint32- 32-bit unsigned (0 to 4,294,967,295)uint64- 64-bit unsigned (0 to 18,446,744,073,709,551,615)uintptr- Unsigned integer large enough to hold a pointer
Arbitrary-precision Integers
bigint- Arbitrary-precision signed integer backed bymath/big.Int.
bigint is useful when your values can exceed 64-bit ranges or when you need exact integer math independent of machine word size.
You construct bigint values with the global bigint(...) function:
Constructor rules:
- Accepted inputs:
- Fixed-size integer families:
int,int8,int16,int32,int64,uint,uint8,uint16,uint32,uint64,uintptr. -
Strings containing an optional sign followed by:
- Decimal digits, e.g.
"123","-42". - Hex with
0x/0Xprefix, e.g."0xff","-0x10". - Binary with
0b/0Bprefix, e.g."0b1010","-0b11".
- Decimal digits, e.g.
-
Rejected inputs:
- Floats (
float32,float64). - Booleans.
- Empty strings or strings without digits after an optional sign or prefix.
- Non-integer strings like
"12.3","0x","0b".
Invalid inputs cause runtime errors with descriptive messages.
Arbitrary-precision Decimals
bigdecimal- Arbitrary-precision decimal/rational number backed bymath/big.Rat.
bigdecimal is useful when you need exact decimal math (for example, money, interest rates, or fractions like 1/3) without binary floating-point rounding error.
You construct bigdecimal values with the global bigdecimal(...) function:
Constructor rules:
- Accepted inputs:
- Fixed-size integers:
int,int8,int16,int32,int64,uint,uint8,uint16,uint32,uint64,uintptr. float32,float64(converted via Go'sbig.Rat.SetFloat64, which may round when necessary).- Strings containing an optional sign and one of:
- Decimal form, e.g.
"0.1","-2.75". - Rational form
p/q, e.g."1/3","-22/7".
- Decimal form, e.g.
- Rejected inputs:
- Booleans.
- Empty strings or malformed numeric/rational strings.
Invalid inputs cause runtime errors with descriptive messages.
bigdecimal supports the same arithmetic operators as fixed-size numbers, but only in same-type operations:
bigdecimal + bigdecimal,-,*,/,%,</,**
Division (/) produces an exact rational result. Floor division (</) and modulo (%) follow the identity:
bigdecimal also provides instance methods mirroring integers and floats:
.abs(),.min(other),.max(other),.clamp(min, max),.toString()
Float Types
float32- 32-bit IEEE-754 floating pointfloat64- 64-bit IEEE-754 floating point (default for float literals)
Integer Instance Methods
All integer methods return values of the same type as the receiver.
abs()
Returns the absolute value:
| abs() | |
|---|---|
min(other)
Returns the minimum of two integers:
| min() | |
|---|---|
max(other)
Returns the maximum of two integers:
| max() | |
|---|---|
clamp(min, max)
Restricts the value to be within the specified range:
| clamp() | |
|---|---|
toString()
Converts the integer to a string:
| toString() | |
|---|---|
Float Instance Methods
All float methods return float64 values.
abs()
Returns the absolute value:
min(other)
Returns the minimum of two floats:
max(other)
Returns the maximum of two floats:
clamp(min, max)
Restricts the value to be within the specified range:
| clamp() | |
|---|---|
round()
Rounds to the nearest integer value:
| round() | |
|---|---|
floor()
Rounds down to the nearest integer:
| floor() | |
|---|---|
ceil()
Rounds up to the nearest integer:
| ceil() | |
|---|---|
isNaN()
Checks if the value is Not-a-Number:
| isNaN() | |
|---|---|
isInf()
Checks if the value is positive or negative infinity:
| isInf() | |
|---|---|
toString()
Converts the float to a string:
| toString() | |
|---|---|
None-Receiver Safety
All number instance methods enforce None-receiver safety. Calling a method on None raises a runtime error:
| None-Receiver Safety | |
|---|---|
Number Literals
| Number Literals | |
|---|---|
Complete Example
Use Cases
Value Validation
Range Constraints
| Range Constraints | |
|---|---|
Rounding Calculations
| Rounding Calculations | |
|---|---|
String Conversion
| String Conversion | |
|---|---|
See Also
- Data Types - Overview of all data types
- Operators and Expressions - Arithmetic operations
- Advanced Arithmetic Operators - Advanced math operations