### Example Header Macro Source: https://docs.rs/fastnum/latest/src/fastnum/bint/doc.rs.html Generates a standard header for code examples, including a note about the example being shared across integer types and a link to the specific type. It also includes the necessary `use` statement for `fastnum::*`. ```rust macro_rules! example_header { ($sign: ident $bits: literal) => { concat! ("\n\n# Examples\n\nPlease note that this example is shared between integer types.\nWhich explains why ", doc::link_type_str!($sign $bits), " is used here.", "\n\n```\nuse fastnum::*"; } } pub(crate) use example_header; ``` -------------------------------- ### Example Header Macro Source: https://docs.rs/fastnum/latest/src/fastnum/decimal/doc.rs.html Macro to generate a standard header for code examples, indicating that the example is shared across decimal types and uses a generic type link. ```rust macro_rules! example_header { ($bits: literal $($sign: ident)?) => { concat!( "# Examples" "Please note that this example is shared between decimal types." "Which explains why ", doc::link_type_str!($bits $($sign)?), " is used here.") } } pub(crate) use example_header; ``` -------------------------------- ### Comparison and Ordering Examples Source: https://docs.rs/fastnum/latest/fastnum/index.html Shows examples of equality, inequality, greater than, less than, and total ordering predicates for decimal numbers, including special values like Infinity and NaN. Ensure the fastnum crate is imported. ```rust use fastnum::*; assert!(dec256!(0.2) == dec256!(0.2)); assert!(dec256!(0.2) > dec256!(0.1)); assert!(dec256!(0.1) < dec256!(0.3)); assert!(D256::MAX < D256::INFINITY); assert!(D256::INFINITY < D256::NAN); assert!(D256::NAN != D256::NAN); ``` -------------------------------- ### Reverse Bits Example for I256 Source: https://docs.rs/fastnum/latest/fastnum/bint/type.I128.html Demonstrates the `reverse_bits` method. This example is shared across integer types, using `I256` for illustration. ```rust use fastnum::* let n = i256!(0x12345678901234567890123456789012); assert_eq!(n.reverse_bits().reverse_bits(), n); ``` -------------------------------- ### Get Number of Bits for an Integer Source: https://docs.rs/fastnum/latest/fastnum/bint/type.I128.html Demonstrates how to get the number of bits set in an integer. This example uses `i256` and `I256::ZERO`. ```rust use fastnum::* assert_eq!(i256!(0b1111001010100).bits(), 13); assert_eq!(I256::ZERO.bits(), 0); ``` -------------------------------- ### Get Number of Bits for an Integer Source: https://docs.rs/fastnum/latest/fastnum/bint/type.I64.html Demonstrates how to get the number of bits set in an integer. This example uses `i256!` macro and `I256::ZERO`. ```rust use fastnum::* assert_eq!(i256!(0b1111001010100).bits(), 13); assert_eq!(I256::ZERO.bits(), 0); ``` -------------------------------- ### Exponentiation by Squaring Example for I256 Source: https://docs.rs/fastnum/latest/fastnum/bint/type.I128.html Illustrates the `pow` method for exponentiation. This example is shared across integer types, using `I256` for illustration. ```rust use fastnum::* let n = i256!(3); assert_eq!(n.pow(5), i256!(243)); ``` -------------------------------- ### Example: Strict Power of Five Source: https://docs.rs/fastnum/latest/fastnum/bint/type.U1024.html Demonstrates calculating 5 to the power of 2 using `strict_power_of_five`. This example is shared across integer types. ```rust use fastnum::* assert_eq!(U256::strict_power_of_five(2), u256!(25)); ``` -------------------------------- ### Integer Base 10 Logarithm Example for I256 Source: https://docs.rs/fastnum/latest/fastnum/bint/type.I128.html Demonstrates calculating the integer base 10 logarithm using `ilog10`. This example is shared across integer types, using `I256` for illustration. ```rust use fastnum::* let n = i256!(150); assert_eq!(n.ilog10(), 2); ``` -------------------------------- ### Constructing U1024 with u1024 Macro Source: https://docs.rs/fastnum/latest/fastnum/macro.u1024.html Usage examples for creating and asserting values using the u1024 macro. ```rust use fastnum::{u1024, U1024}; const N: U1024 = u1024!(100); let x = u1024!(1); assert!(u1024!(0).is_zero()); println!("{x}"); ``` -------------------------------- ### Resize UnsignedDecimal Source: https://docs.rs/fastnum/latest/fastnum/decimal/struct.UnsignedDecimal.html Examples demonstrating lossless widening and narrowing with potential rounding for UnsignedDecimal types. ```rust use fastnum::*; let x = udec64!(123.45); // Increase internal width from 2 to 4 limbs — value is preserved. let y: UD128 = x.resize(); assert_eq!(y, udec128!(123.45)); assert!(y.is_op_ok()); ``` ```rust use fastnum::*; let x = udec128!(1.8446744073709551616); // Reduce width; value may be rounded according to context. let y: UD64 = x.resize(); // Rounding/precision-loss indicators may be set, depending on capacity and context: assert_eq!(y, udec64!(1.844674407370955162)); assert!(y.is_op_inexact() && y.is_op_rounded()); ``` -------------------------------- ### Example: Signum of i256 Source: https://docs.rs/fastnum/latest/fastnum/bint/type.I64.html Illustrates the signum function for positive, zero, and negative i256 values. ```rust use fastnum::* assert_eq!(i256!(10).signum(), i256!(1)); assert_eq!(i256!(0).signum(), i256!(0)); assert_eq!(i256!(-10).signum(), i256!(-1)); ``` -------------------------------- ### Convert i32 to Decimal Source: https://docs.rs/fastnum/latest/fastnum/decimal/type.D512.html Example of converting an i32 primitive to a Decimal type. ```rust pub const fn from_i32(n: i32) -> Self ``` -------------------------------- ### Convert i64 to Decimal Source: https://docs.rs/fastnum/latest/fastnum/decimal/type.D512.html Example of converting an i64 primitive to a Decimal type. ```rust pub const fn from_i64(n: i64) -> Self ``` -------------------------------- ### Perform Addition and Subtraction Source: https://docs.rs/fastnum/latest/fastnum/index.html Examples of addition and subtraction operations, illustrating how exponents and precision are handled. ```rust use fastnum::{udec256, dec256}; assert_eq!(udec256!(12) + udec256!(7.00), udec256!(19.00)); assert_eq!(udec256!(1E+2) + udec256!(1E+4), udec256!(1.01E+4)); assert_eq!(udec256!(1.3) - udec256!(1.07), udec256!(0.23)); assert_eq!(udec256!(1.3) - udec256!(1.30), udec256!(0.00)); assert_eq!(dec256!(1.3) - dec256!(2.07), dec256!(-0.77)); ``` -------------------------------- ### Convert u64 to Decimal Source: https://docs.rs/fastnum/latest/fastnum/decimal/type.D512.html Example of converting a u64 primitive to a Decimal type. ```rust pub const fn from_u64(n: u64) -> Self ``` -------------------------------- ### Constructing U256 Literals Source: https://docs.rs/fastnum/latest/fastnum/macro.u256.html Usage examples for the u256 macro, including constant initialization and runtime usage. ```rust use fastnum::{u256, U256}; const N: U256 = u256!(100); let x = u256!(1); assert!(u256!(0).is_zero()); println!("{x}"); ``` -------------------------------- ### Construct I64 instances with i64 macro Source: https://docs.rs/fastnum/latest/fastnum/macro.i64.html Usage examples for creating I64 values from literals and performing basic assertions. ```rust use fastnum::{i64, I64}; const N: I64 = i64!(100); let x = i64!(1); assert!(i64!(0).is_zero()); println!("{x}"); ``` -------------------------------- ### Example: Division by Zero with Custom Context Source: https://docs.rs/fastnum/latest/src/fastnum/decimal/context.rs.html Demonstrates how to use a custom `Context` to handle division by zero without panicking. The resulting `Decimal` indicates infinite value and specific operation flags. ```rust use fastnum::{*, decimal::*}; let ctx = Context::default().without_traps(); // No panic! We can divide by zero! let res = dec256!(1.0).with_ctx(ctx) / dec256!(0).with_ctx(ctx); assert!(res.is_infinite()); assert!(res.is_op_div_by_zero()); assert!(res.is_op_invalid()); ``` -------------------------------- ### Count Ones Macro for Integer Types Source: https://docs.rs/fastnum/latest/src/fastnum/bint/doc/num.rs.html Use this macro to get the number of set bits (ones) in the binary representation of an integer. Example usage is provided. ```rust macro_rules! count_ones { ($sign: ident $bits: literal) => { doc::doc_comment! { #count_ones, $sign $bits, "Returns the number of ones in the binary representation of `self`.", "let a = " doc::m!($sign $bits) "(7);\n\n" "assert_eq!(a.count_ones(), 3);\n" } }; } pub(crate) use count_ones; ``` -------------------------------- ### Static assertion failure example Source: https://docs.rs/fastnum/latest/fastnum/macro.dec1024.html Example of invalid input that triggers a compile-time panic. ```rust // The below example will fail to compile, as the function will panic at compile time: use fastnum::{dec1024, D1024} // Gives a compile error of "error[E0080]: evaluation of constant value failed... // the evaluated program panicked at 'attempt to parse decimal from string containing invalid digit'", const N: D1024 = dec1024!(A1.23456789); ``` -------------------------------- ### Basic Usage of dec64 Macro Source: https://docs.rs/fastnum/latest/fastnum/macro.dec64.html Demonstrates basic usage of the dec64 macro for creating D64 constants and variables. Includes examples of non-zero, zero, and arithmetic operations. ```rust use fastnum::*; const N: D64 = dec64!(1.23456789); assert!(!N.is_zero()); let num = dec64!(0); assert!(num.is_zero()); const A: D64 = dec64!(5); const B: D64 = dec64!(1_000); const C: D64 = A.div(B); assert_eq!(C, dec64!(0.005)); ``` -------------------------------- ### Fastnum Power Operation Example Source: https://docs.rs/fastnum/latest/fastnum/decimal/struct.Decimal.html Demonstrates the basic usage of the `powi` method for integer powers. Handles positive, negative, and zero exponents. ```rust use fastnum::* assert_eq!(dec256!(2).powi(3), dec256!(8)); assert_eq!(dec256!(9).powi(2), dec256!(81)); assert_eq!(dec256!(1).powi(-2), dec256!(1)); assert_eq!(dec256!(10).powi(20), dec256!(1e20)); assert_eq!(dec256!(4).powi(-2), dec256!(0.0625)); ``` -------------------------------- ### Example: Division by Zero with Context Source: https://docs.rs/fastnum/latest/fastnum/decimal/struct.Context.html Demonstrates using a Context without traps to perform division by zero without panicking. The result is checked for infinity and specific operation flags. ```rust use fastnum::{*, decimal::*}; let ctx = Context::default().without_traps(); // No panic! We can divide by zero! let res = dec256!(1.0).with_ctx(ctx) / dec256!(0).with_ctx(ctx); assert!(res.is_infinite()); assert!(res.is_op_div_by_zero()); assert!(res.is_op_invalid()); ``` -------------------------------- ### Carrying Multiplication Example Source: https://docs.rs/fastnum/latest/fastnum/bint/struct.UInt.html Demonstrates the usage of `carrying_mul` with maximum value inputs. Note that this example uses `U256` for illustration. ```rust use fastnum::* let a = U256::MAX; let b = U256::MAX; let c = U256::ZERO; assert_eq!(a.carrying_mul(b, c), (U256::ONE, b - U256::ONE)); ``` -------------------------------- ### Get Type ID of Context Source: https://docs.rs/fastnum/latest/fastnum/decimal/struct.Context.html Gets the TypeId of the Context. This is part of the Any trait implementation, used for dynamic type checking. ```rust fn type_id(&self) -> TypeId ``` -------------------------------- ### Construct I512 integers using the i512 macro Source: https://docs.rs/fastnum/latest/fastnum/macro.i512.html Usage examples for creating I512 instances from literals, including constant initialization and assertion checks. ```rust use fastnum::{i512, I512}; const N: I512 = i512!(100); let x = i512!(1); assert!(i512!(0).is_zero()); println!("{x}"); ``` -------------------------------- ### Construct I128 instances using i128 macro Source: https://docs.rs/fastnum/latest/fastnum/macro.i128.html Usage examples for creating I128 values from literals and performing basic assertions. ```rust use fastnum::{i128, I128}; const N: I128 = i128!(100); let x = i128!(1); assert!(i128!(0).is_zero()); println!("{x}"); ``` -------------------------------- ### Division Examples with Fastnum Source: https://docs.rs/fastnum/latest/fastnum/index.html Demonstrates various division operations using the dec128! macro. Ensure the fastnum crate is imported. ```rust use fastnum::* assert_eq!(dec128!(1) / dec128!(3), dec128!(0.333333333333333333333333333333333333333)); ``` ```rust assert_eq!(dec128!(2) / dec128!(3), dec128!(0.66666666666666666666666666666666666667)); ``` ```rust assert_eq!(dec128!(5) / dec128!(2), dec128!(2.5)); ``` ```rust assert_eq!(dec128!(1) / dec128!(10), dec128!(0.1)); ``` ```rust assert_eq!(dec128!(12) / dec128!(12), dec128!(1)); ``` ```rust assert_eq!(dec128!(8.00) / dec128!(2), dec128!(4.00)); ``` ```rust assert_eq!(dec128!(2.400) / dec128!(2.0), dec128!(1.20)); ``` ```rust assert_eq!(dec128!(1000) / dec128!(100), dec128!(10)); ``` ```rust assert_eq!(dec128!(1000) / dec128!(1), dec128!(1000)); ``` ```rust assert_eq!(dec128!(2.40E+6) / dec128!(2), dec128!(1.20E+6)); ``` -------------------------------- ### Example: Strict Power of Ten Source: https://docs.rs/fastnum/latest/fastnum/bint/type.U1024.html Demonstrates calculating 10 to the power of 2 using `strict_power_of_ten`. This example is shared across integer types. ```rust use fastnum::* assert_eq!(U256::strict_power_of_ten(2), u256!(100)); ``` -------------------------------- ### Widening Multiplication Example Source: https://docs.rs/fastnum/latest/fastnum/bint/type.U512.html Demonstrates widening multiplication which returns the low and high bits of the product. This example is shared across integer types. ```rust use fastnum::* let a = U256::MAX; let b = U256::MAX; assert_eq!(a.widening_mul(b), (U256::ONE, b - U256::ONE)); ``` -------------------------------- ### Logarithm Function Examples Source: https://docs.rs/fastnum/latest/fastnum/index.html Illustrates the use of natural logarithm (ln), base-10 logarithm (log10), and base-2 logarithm (log2) functions. Requires importing the fastnum crate. ```rust use fastnum::*; assert_eq!(dec256!(2).ln(), D256::LN_2); assert_eq!(dec256!(10).ln(), D256::LN_10); assert_eq!(dec256!(100).log10(), D256::TWO); assert_eq!(dec256!(512).log2(), dec256!(9)); ``` -------------------------------- ### Construct and Use I256 Integers Source: https://docs.rs/fastnum/latest/fastnum/macro.i256.html Demonstrates how to use the i256 macro to create I256 integer constants and variables. Includes examples of checking for zero and printing the value. ```rust use fastnum::{i256, I256}; const N: I256 = i256!(100); let x = i256!(1); assert!(i256!(0).is_zero()); println!("{x}"); ``` -------------------------------- ### Compile-Time Error Example with udec256 Source: https://docs.rs/fastnum/latest/fastnum/macro.udec256.html Illustrates a compile-time error scenario when using udec256 with an invalid literal. This example will fail to compile, demonstrating compile-time validation. ```rust use fastnum::{udec256, UD256} // Gives a compile error of "error[E0080]: evaluation of constant value failed...\n// the evaluated program panicked at 'attempt to parse decimal from string containing invalid digit'", const N: UD256 = udec256!(A1.23456789); ``` -------------------------------- ### TryFrom i64 to UInt Source: https://docs.rs/fastnum/latest/fastnum/bint/struct.UInt.html Attempts to convert an `i64` to a `UInt`. Returns an error if the conversion fails. ```APIDOC ## TryFrom i64 to UInt ### Description Attempts to convert an `i64` to a `UInt`. Returns an error if the conversion fails. ### Method `try_from` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **n** (i64) - Required - The signed integer to convert. ### Request Example ```json { "n": 9223372036854775807 } ``` ### Response #### Success Response (200) - **value** (UInt) - The converted UInt value. #### Error Response (400) - **error** (ParseError) - Indicates a conversion error (e.g., negative input). #### Response Example ```json { "value": " value>" } ``` ``` -------------------------------- ### Basic Usage of udec64 Macro Source: https://docs.rs/fastnum/latest/fastnum/macro.udec64.html Demonstrates basic construction of UD64 values using the udec64 macro. Ensure the fastnum crate is imported. Values are const-evaluated. ```rust use fastnum::* const N: UD64 = udec64!(1.23456789); assert!(!N.is_zero()); let num = udec64!(0); assert!(num.is_zero()); const A: UD64 = udec64!(5); const B: UD64 = udec64!(1_000); const C: UD64 = A.div(B); assert_eq!(C, udec64!(0.005)); ``` -------------------------------- ### Carrying Multiplication Example Source: https://docs.rs/fastnum/latest/fastnum/bint/type.U512.html Demonstrates carrying multiplication for large integers, calculating `self * rhs + carry`. This example uses U256 and is shared across integer types. ```rust use fastnum::*; let a = U256::MAX; let b = U256::MAX; let c = U256::ZERO; assert_eq!(a.carrying_mul(b, c), (U256::ONE, b - U256::ONE)); ``` -------------------------------- ### I512 Bit Count Assertion Source: https://docs.rs/fastnum/latest/fastnum/bint/type.I256.html Example verifying the total number of bits for a specific integer type. This example uses I512 and is shared across integer types. ```rust use fastnum::* assert_eq!(I512::BITS, 512); ``` -------------------------------- ### Use dec1024 for constant initialization Source: https://docs.rs/fastnum/latest/fastnum/macro.dec1024.html Demonstrates creating D1024 constants and performing compile-time arithmetic. ```rust use fastnum::*; const N: D1024 = dec1024!(1.23456789); assert!(!N.is_zero()); let num = dec1024!(0); assert!(num.is_zero()); const A: D1024 = dec1024!(5); const B: D1024 = dec1024!(1_000); const C: D1024 = A.div(B); assert_eq!(C, dec1024!(0.005)); ``` -------------------------------- ### Is Power of Two Example for I256 Source: https://docs.rs/fastnum/latest/fastnum/bint/type.I128.html Shows how to check if a number is a power of two using the `is_power_of_two` method. This example is shared across integer types, using `I256` for illustration. ```rust use fastnum::* let n = i256!(8); assert!(n.is_power_of_two()); let m = i256!(90); assert!(!m.is_power_of_two()); ``` -------------------------------- ### Example of Handling ParseError Source: https://docs.rs/fastnum/latest/fastnum/decimal/enum.ParseError.html Demonstrates how to use a try-catch block to handle potential ParseError when converting a string to a UD256 type. This example shows error handling for invalid input strings. ```rust use fastnum::decimal::Context; use fastnum::UD256; if let Err(e) = UD256::from_str("e12", Context::default()) { println!("Failed conversion to Decimal: {e}"); } ``` -------------------------------- ### Binary Exponential Function Examples Source: https://docs.rs/fastnum/latest/fastnum/index.html Demonstrates the usage of the exp2 function for various special values and standard numbers. Ensure the fastnum crate is imported. ```rust use fastnum::*; assert_eq!(D128::NEG_INFINITY.exp2(), dec128!(0)); assert!(D128::INFINITY.exp2().is_infinite()); assert_eq!(dec128!(0).exp2(), dec128!(1)); assert_eq!(dec128!(1).exp2(), dec128!(2)); assert_eq!(dec128!(2).exp2(), dec128!(4)); ``` -------------------------------- ### Initialize signals with the signals! macro Source: https://docs.rs/fastnum/latest/src/fastnum/decimal/macros/signals.rs.html Use this macro to combine multiple decimal operation signals. Requires importing the fastnum crate and decimal module. ```rust use fastnum::{*, decimal::*}; let signals = signals![!OFW, !CP]; assert_eq!(signals, Signals::OP_OVERFLOW.combine(Signals::OP_CLAMPED)); ``` ```rust macro_rules! signals { [ $(! $tts: tt),* ] => {{ const __SIGNALS: $crate::decimal::Signals = signals!(@ [$($tts),*]); __SIGNALS }}; (@ []) => { $crate::decimal::Signals::EMPTY }; (@ CP) => { $crate::decimal::Signals::OP_CLAMPED }; (@ DBZ) => { $crate::decimal::Signals::OP_DIV_BY_ZERO }; (@ INEXACT) => { $crate::decimal::Signals::OP_INEXACT }; (@ INV) => { $crate::decimal::Signals::OP_INVALID }; (@ OFW) => { $crate::decimal::Signals::OP_OVERFLOW }; (@ ROUND) => { $crate::decimal::Signals::OP_ROUNDED }; (@ SN) => { $crate::decimal::Signals::OP_SUBNORMAL }; (@ UFW) => { $crate::decimal::Signals::OP_UNDERFLOW }; (@ [$t:tt]) => { signals!(@ $t) }; (@ [$t:tt, $($tts:tt),*]) => { signals!(@ $t).combine(signals!(@ [$($tts),*])) }; } ``` -------------------------------- ### Multiplication Examples with Fastnum Source: https://docs.rs/fastnum/latest/fastnum/index.html Demonstrates various multiplication operations using the dec128! macro. Ensure the fastnum crate is imported. ```rust use fastnum::* assert_eq!(dec128!(1.20) * dec128!(3), dec128!(3.60)); ``` ```rust assert_eq!(dec128!(7) * dec128!(3), dec128!(21)); ``` ```rust assert_eq!(dec128!(0.9) * dec128!(0.8), dec128!(0.72)); ``` ```rust assert_eq!(dec128!(0.9) * dec128!(-0), dec128!(-0.0)); ``` ```rust assert_eq!(dec128!(654321) * dec128!(654321), dec128!(4.28135971041E+11)); ``` -------------------------------- ### I512 Minimum Value Assertion Source: https://docs.rs/fastnum/latest/fastnum/bint/type.I256.html Example demonstrating the relationship between the minimum and maximum values for a signed integer type. Note that this example uses I512 for demonstration purposes and is shared across integer types. ```rust use fastnum::* assert_eq!(!I512::MIN, I512::MAX); ``` -------------------------------- ### TryFrom UInt to i64 Source: https://docs.rs/fastnum/latest/fastnum/bint/struct.UInt.html Attempts to convert a `UInt` to an `i64`. Returns an error if the conversion fails. ```APIDOC ## TryFrom UInt to i64 ### Description Attempts to convert a `UInt` to an `i64`. Returns an error if the conversion fails. ### Method `try_from` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **n** (UInt) - Required - The unsigned integer to convert. ### Request Example ```json { "n": " value>" } ``` ### Response #### Success Response (200) - **value** (i64) - The converted i64 value. #### Error Response (400) - **error** (ParseError) - Indicates a conversion error. #### Response Example ```json { "value": 9223372036854775807 } ``` ``` -------------------------------- ### TryFrom u64 to UInt Source: https://docs.rs/fastnum/latest/fastnum/bint/struct.UInt.html Attempts to convert a `u64` to a `UInt`. Returns an error if the conversion fails. ```APIDOC ## TryFrom u64 to UInt ### Description Attempts to convert a `u64` to a `UInt`. Returns an error if the conversion fails. ### Method `try_from` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **n** (u64) - Required - The unsigned integer to convert. ### Request Example ```json { "n": 18446744073709551615 } ``` ### Response #### Success Response (200) - **value** (UInt) - The converted UInt value. #### Error Response (400) - **error** (ParseError) - Indicates a conversion error. #### Response Example ```json { "value": " value>" } ``` ``` -------------------------------- ### I512 Byte Count Assertion Source: https://docs.rs/fastnum/latest/fastnum/bint/type.I256.html Example confirming the number of bytes required to represent a specific integer type. This calculation is derived from the total bits. Note that this example uses I512 and is shared across integer types. ```rust use fastnum::* assert_eq!(I512::BYTES, 512 / 8); ``` -------------------------------- ### Wrapping Next Power of Two Example Source: https://docs.rs/fastnum/latest/fastnum/bint/type.U512.html Calculates the smallest power of two greater than or equal to self. If the next power of two exceeds Self::MAX, it wraps to Self::MIN. This example is shared across integer types. ```rust use fastnum::* let n = u256!(31); assert_eq!(n.wrapping_next_power_of_two(), 32u32.into()); assert_eq!(U256::MAX.wrapping_next_power_of_two(), U256::MIN); ``` -------------------------------- ### Basic Usage of udec1024 Macro Source: https://docs.rs/fastnum/latest/fastnum/macro.udec1024.html Demonstrates basic usage of the udec1024 macro for creating UD1024 constants and variables, including initialization from literals and performing division. ```rust use fastnum::*; const N: UD1024 = udec1024!(1.23456789); assert!(!N.is_zero()); let num = udec1024!(0); assert!(num.is_zero()); const A: UD1024 = udec1024!(5); const B: UD1024 = udec1024!(1_000); const C: UD1024 = A.div(B); assert_eq!(C, udec1024!(0.005)); ``` -------------------------------- ### Initialize Signals Set using signals! Macro Source: https://docs.rs/fastnum/latest/fastnum/macro.signals.html Use the `signals!` macro to initialize a set of signals. Ensure the necessary imports (`fastnum::*`, `fastnum::decimal::*`) are present. This example combines the `OP_OVERFLOW` and `OP_CLAMPED` signals. ```rust use fastnum::{*, decimal::*}; let signals = signals![!OFW, !CP]; assert_eq!(signals, Signals::OP_OVERFLOW.combine(Signals::OP_CLAMPED)); ``` -------------------------------- ### Carrying Multiplication Source: https://docs.rs/fastnum/latest/fastnum/bint/type.U1024.html Example usage of carrying_mul for full multiplication. ```rust use fastnum::*;\n\nlet a = U256::MAX;\nlet b = U256::MAX;\nlet c = U256::ZERO;\n\nassert_eq!(a.carrying_mul(b, c), (U256::ONE, b - U256::ONE)); ``` -------------------------------- ### I512 Maximum Value Wrapping Addition Source: https://docs.rs/fastnum/latest/fastnum/bint/type.I256.html Example showing the wrapping behavior when adding one to the maximum value of a signed integer type. This demonstrates that MAX + ONE results in MIN due to overflow. Note that this example uses I512 and is shared across integer types. ```rust use fastnum::* assert_eq!(I512::MAX.wrapping_add(I512::ONE), I512::MIN); ``` -------------------------------- ### TryFrom i32 to UInt Source: https://docs.rs/fastnum/latest/fastnum/bint/struct.UInt.html Attempts to convert an `i32` to a `UInt`. Returns an error if the conversion fails. ```APIDOC ## TryFrom i32 to UInt ### Description Attempts to convert an `i32` to a `UInt`. Returns an error if the conversion fails. ### Method `try_from` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **n** (i32) - Required - The signed integer to convert. ### Request Example ```json { "n": 2147483647 } ``` ### Response #### Success Response (200) - **value** (UInt) - The converted UInt value. #### Error Response (400) - **error** (ParseError) - Indicates a conversion error (e.g., negative input). #### Response Example ```json { "value": " value>" } ``` ``` -------------------------------- ### Unsigned Integer BYTES Constant Source: https://docs.rs/fastnum/latest/fastnum/bint/type.U1024.html Example usage of the BYTES constant. ```rust use fastnum::*;\n\nassert_eq!(U512::BYTES, 512 / 8); ``` -------------------------------- ### Unsigned Integer BITS Constant Source: https://docs.rs/fastnum/latest/fastnum/bint/type.U1024.html Example usage of the BITS constant. ```rust use fastnum::*;\n\nassert_eq!(U512::BITS, 512); ``` -------------------------------- ### TryFrom i8 to UInt Source: https://docs.rs/fastnum/latest/fastnum/bint/struct.UInt.html Attempts to convert an `i8` to a `UInt`. Returns an error if the conversion fails. ```APIDOC ## TryFrom i8 to UInt ### Description Attempts to convert an `i8` to a `UInt`. Returns an error if the conversion fails. ### Method `try_from` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **n** (i8) - Required - The signed integer to convert. ### Request Example ```json { "n": 127 } ``` ### Response #### Success Response (200) - **value** (UInt) - The converted UInt value. #### Error Response (400) - **error** (ParseError) - Indicates a conversion error (e.g., negative input). #### Response Example ```json { "value": " value>" } ``` ``` -------------------------------- ### Construct I1024 Integers with i1024 Macro Source: https://docs.rs/fastnum/latest/fastnum/macro.i1024.html Demonstrates how to use the i1024 macro to create I1024 signed integers. Ensure the fastnum crate is imported. ```rust use fastnum::{i1024, I1024}; const N: I1024 = i1024!(100); let x = i1024!(1); assert!(i1024!(0).is_zero()); println!("{x}"); ``` -------------------------------- ### Decimal Default Value Source: https://docs.rs/fastnum/latest/fastnum/decimal/type.D512.html Describes how to get the default value for Decimal. ```APIDOC ## GET /decimal/default ### Description Returns the default value for the Decimal type, which is typically zero. ### Method GET ### Endpoint `/decimal/default` ### Response #### Success Response (200) - **Decimal** - The default Decimal value. #### Response Example ```json { "value": "0" } ``` ``` -------------------------------- ### Install fastnum dependency Source: https://docs.rs/fastnum/latest/fastnum/index.html Add the fastnum crate to your Cargo.toml file. ```toml fastnum = "0.7" ``` ```toml fastnum = { version = "0.7", features = ["serde"] } # enables the "serde" feature ``` -------------------------------- ### Addition and Subtraction Source: https://docs.rs/fastnum/latest/fastnum Logic and examples for performing addition and subtraction on decimal types. ```APIDOC ## Addition and Subtraction ### Description Performs addition or subtraction on decimal operands. If operands are special values (Infinity, NaN), specific rules apply. Otherwise, coefficients are aligned based on exponents before calculation. ### Request Example ```rust use fastnum::{udec256, dec256}; assert_eq!(udec256!(12) + udec256!(7.00), udec256!(19.00)); assert_eq!(udec256!(1.3) - udec256!(1.07), udec256!(0.23)); ``` ``` -------------------------------- ### TryFrom i16 to UInt Source: https://docs.rs/fastnum/latest/fastnum/bint/struct.UInt.html Attempts to convert an `i16` to a `UInt`. Returns an error if the conversion fails. ```APIDOC ## TryFrom i16 to UInt ### Description Attempts to convert an `i16` to a `UInt`. Returns an error if the conversion fails. ### Method `try_from` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **n** (i16) - Required - The signed integer to convert. ### Request Example ```json { "n": 32767 } ``` ### Response #### Success Response (200) - **value** (UInt) - The converted UInt value. #### Error Response (400) - **error** (ParseError) - Indicates a conversion error (e.g., negative input). #### Response Example ```json { "value": " value>" } ``` ``` -------------------------------- ### UD256 Zero Check Source: https://docs.rs/fastnum/latest/fastnum/decimal/type.UD64.html Provides examples of checking if a UD256 number is zero. ```APIDOC ## UD256 is_zero() ### Description Checks if the referenced unsigned decimal is zero. ### Method `is_zero(&self) -> bool` ### Endpoint N/A (Rust method) ### Request Body N/A ### Request Example ```rust use fastnum::{udec256}; let a = udec256!(0); assert!(a.is_zero()); let b = udec256!(0.0); assert!(b.is_zero()); let c = udec256!(0.00); assert!(c.is_zero()); let d = udec256!(0.1); assert!(!d.is_zero()); ``` ### Response N/A (Example shows assertions) ``` -------------------------------- ### Fastnum Basic Usage Example Source: https://docs.rs/fastnum/latest/fastnum/decimal/type.D512.html Demonstrates basic usage of the fastnum crate for arithmetic operations, specifically a fused multiply-add. Ensure the 'fastnum' crate is imported. ```rust use fastnum::*; assert_eq!(dec128!(10.0).mul_add(dec128!(4.0), dec128!(60)), dec128!(100)); ``` -------------------------------- ### Use dec512 for constant and variable initialization Source: https://docs.rs/fastnum/latest/fastnum/macro.dec512.html Demonstrates creating D512 constants and variables from literals, including performing arithmetic operations at compile time. ```rust use fastnum::*; const N: D512 = dec512!(1.23456789); assert!(!N.is_zero()); let num = dec512!(0); assert!(num.is_zero()); const A: D512 = dec512!(5); const B: D512 = dec512!(1_000); const C: D512 = A.div(B); assert_eq!(C, dec512!(0.005)); ``` -------------------------------- ### Handle compile-time errors Source: https://docs.rs/fastnum/latest/fastnum/index.html Examples of invalid compile-time operations that will trigger errors. ```rust use fastnum::*; // Invalid character. const E: UD256 = udec256!(A3.5); ``` ```rust use fastnum::*; // Arithmetic error during calculation. const E: UD256 = udec256!(1.5).div(udec256!(0)); ``` -------------------------------- ### Convert f64 to Decimal Source: https://docs.rs/fastnum/latest/fastnum/decimal/type.D512.html Example of converting an f64 primitive to a Decimal type. ```rust pub const fn from_f64(n: f64) -> Self ``` -------------------------------- ### Convert f32 to Decimal Source: https://docs.rs/fastnum/latest/fastnum/decimal/type.D512.html Example of converting an f32 primitive to a Decimal type. ```rust pub const fn from_f32(n: f32) -> Self ```