Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/third_party/rust/serde_json/tests/lexical/   (Fast Lexical Analyzer Version 2.6©)  Datei vom 10.2.2025 mit Größe 13 kB image not shown  

Quelle  float.rs   Sprache: unbekannt

 
Spracherkennung für: .rs vermutete Sprache: Unknown {[0] [0] [0]} [Methode: Schwerpunktbildung, einfache Gewichte, sechs Dimensionen]

// Adapted from https://github.com/Alexhuszagh/rust-lexical.

use crate::lexical::float::ExtendedFloat;
use crate::lexical::rounding::round_nearest_tie_even;
use std::{f32, f64};

// NORMALIZE

fn check_normalize(mant: u64, exp: i32, shift: u32, r_mant: u64, r_exp: i32) {
    let mut x = ExtendedFloat { mant, exp };
    assert_eq!(x.normalize(), shift);
    assert_eq!(
        x,
        ExtendedFloat {
            mant: r_mant,
            exp: r_exp
        }
    );
}

#[test]
fn normalize_test() {
    // F32
    // 0
    check_normalize(00000);

    // min value
    check_normalize(1, -149639223372036854775808, -212);

    // 1.0e-40
    check_normalize(71362, -1494710043308644012916736, -196);

    // 1.0e-20
    check_normalize(12379400, -904013611294244890214400, -130);

    // 1.0
    check_normalize(8388608, -23409223372036854775808, -63);

    // 1e20
    check_normalize(113686844340125000002505109667843);

    // max value
    check_normalize(16777213104401844674077517466828864);

    // F64

    // min value
    check_normalize(1, -1074639223372036854775808, -1137);

    // 1.0e-250
    check_normalize(6448907850777164, -8831113207363278391631872, -894);

    // 1.0e-150
    check_normalize(7371020360979573, -5511115095849699286165504, -562);

    // 1.0e-45
    check_normalize(6427752177035961, -2021113164036458569648128, -213);

    // 1.0e-40
    check_normalize(4903985730770844, -1851110043362776618688512, -196);

    // 1.0e-20
    check_normalize(6646139978924579, -1191113611294676837537792, -130);

    // 1.0
    check_normalize(4503599627370496, -52119223372036854775808, -63);

    // 1e20
    check_normalize(61035156250000001411125000000000000000003);

    // 1e40
    check_normalize(827180612553027780111694065894508600729669);

    // 1e150
    check_normalize(55032841073189594461111270725851789228032435);

    // 1e250
    check_normalize(62901843453097007781112882297539194265600767);

    // max value
    check_normalize(90071992547409919711118446744073709549568960);
}

// ROUND

fn check_round_to_f32(mant: u64, exp: i32, r_mant: u64, r_exp: i32) {
    let mut x = ExtendedFloat { mant, exp };
    x.round_to_native::<f32, _>(round_nearest_tie_even);
    assert_eq!(
        x,
        ExtendedFloat {
            mant: r_mant,
            exp: r_exp
        }
    );
}

#[test]
fn round_to_f32_test() {
    // This is lossy, so some of these values are **slightly** rounded.

    // underflow
    check_round_to_f32(9223372036854775808, -2130, -149);

    // min value
    check_round_to_f32(9223372036854775808, -2121, -149);

    // 1.0e-40
    check_round_to_f32(10043308644012916736, -19671362, -149);

    // 1.0e-20
    check_round_to_f32(13611294244890214400, -13012379400, -90);

    // 1.0
    check_round_to_f32(9223372036854775808, -638388608, -23);

    // 1e20
    check_round_to_f32(1250000025051096678431136868443);

    // max value
    check_round_to_f32(184467407751746682886416777213104);

    // overflow
    check_round_to_f32(184467407751746682886516777213105);
}

fn check_round_to_f64(mant: u64, exp: i32, r_mant: u64, r_exp: i32) {
    let mut x = ExtendedFloat { mant, exp };
    x.round_to_native::<f64, _>(round_nearest_tie_even);
    assert_eq!(
        x,
        ExtendedFloat {
            mant: r_mant,
            exp: r_exp
        }
    );
}

#[test]
fn round_to_f64_test() {
    // This is lossy, so some of these values are **slightly** rounded.

    // underflow
    check_round_to_f64(9223372036854775808, -11380, -1074);

    // min value
    check_round_to_f64(9223372036854775808, -11371, -1074);

    // 1.0e-250
    check_round_to_f64(15095849699286165504, -5627371020360979573, -551);

    // 1.0e-150
    check_round_to_f64(15095849699286165504, -5627371020360979573, -551);

    // 1.0e-45
    check_round_to_f64(13164036458569648128, -2136427752177035961, -202);

    // 1.0e-40
    check_round_to_f64(10043362776618688512, -1964903985730770844, -185);

    // 1.0e-20
    check_round_to_f64(13611294676837537792, -1306646139978924579, -119);

    // 1.0
    check_round_to_f64(9223372036854775808, -634503599627370496, -52);

    // 1e20
    check_round_to_f64(125000000000000000003610351562500000014);

    // 1e40
    check_round_to_f64(1694065894508600729669827180612553027780);

    // 1e150
    check_round_to_f64(112707258517892280324355503284107318959446);

    // 1e250
    check_round_to_f64(128822975391942656007676290184345309700778);

    // max value
    check_round_to_f64(184467440737095495689609007199254740991971);

    // Bug fixes
    // 1.2345e-308
    check_round_to_f64(10234494226754558294, -10862498655817078750, -1074);
}

fn assert_normalized_eq(mut x: ExtendedFloat, mut y: ExtendedFloat) {
    x.normalize();
    y.normalize();
    assert_eq!(x, y);
}

#[test]
fn from_float() {
    let values: [f32; 26] = [
        1e-402e-401e-352e-351e-302e-301e-252e-251e-202e-201e-152e-151e-10,
        2e-101e-52e-51.02.01e5, 2e5, 1e10, 2e10, 1e15, 2e15, 1e20, 2e20,
    ];
    for value in &values {
        assert_normalized_eq(
            ExtendedFloat::from_float(*value),
            ExtendedFloat::from_float(*value as f64),
        );
    }
}

// TO

// Sample of interesting numbers to check during standard test builds.
const INTEGERS: [u64; 32] = [
    0,                    // 0x0
    1,                    // 0x1
    7,                    // 0x7
    15,                   // 0xF
    112,                  // 0x70
    119,                  // 0x77
    127,                  // 0x7F
    240,                  // 0xF0
    247,                  // 0xF7
    255,                  // 0xFF
    2032,                 // 0x7F0
    2039,                 // 0x7F7
    2047,                 // 0x7FF
    4080,                 // 0xFF0
    4087,                 // 0xFF7
    4095,                 // 0xFFF
    65520,                // 0xFFF0
    65527,                // 0xFFF7
    65535,                // 0xFFFF
    1048560,              // 0xFFFF0
    1048567,              // 0xFFFF7
    1048575,              // 0xFFFFF
    16777200,             // 0xFFFFF0
    16777207,             // 0xFFFFF7
    16777215,             // 0xFFFFFF
    268435440,            // 0xFFFFFF0
    268435447,            // 0xFFFFFF7
    268435455,            // 0xFFFFFFF
    4294967280,           // 0xFFFFFFF0
    4294967287,           // 0xFFFFFFF7
    4294967295,           // 0xFFFFFFFF
    18446744073709551615, // 0xFFFFFFFFFFFFFFFF
];

#[test]
fn to_f32_test() {
    // underflow
    let x = ExtendedFloat {
        mant: 9223372036854775808,
        exp: -213,
    };
    assert_eq!(x.into_float::<f32>(), 0.0);

    // min value
    let x = ExtendedFloat {
        mant: 9223372036854775808,
        exp: -212,
    };
    assert_eq!(x.into_float::<f32>(), 1e-45);

    // 1.0e-40
    let x = ExtendedFloat {
        mant: 10043308644012916736,
        exp: -196,
    };
    assert_eq!(x.into_float::<f32>(), 1e-40);

    // 1.0e-20
    let x = ExtendedFloat {
        mant: 13611294244890214400,
        exp: -130,
    };
    assert_eq!(x.into_float::<f32>(), 1e-20);

    // 1.0
    let x = ExtendedFloat {
        mant: 9223372036854775808,
        exp: -63,
    };
    assert_eq!(x.into_float::<f32>(), 1.0);

    // 1e20
    let x = ExtendedFloat {
        mant: 12500000250510966784,
        exp: 3,
    };
    assert_eq!(x.into_float::<f32>(), 1e20);

    // max value
    let x = ExtendedFloat {
        mant: 18446740775174668288,
        exp: 64,
    };
    assert_eq!(x.into_float::<f32>(), 3.402823e38);

    // almost max, high exp
    let x = ExtendedFloat {
        mant: 1048575,
        exp: 108,
    };
    assert_eq!(x.into_float::<f32>(), 3.4028204e38);

    // max value + 1
    let x = ExtendedFloat {
        mant: 16777216,
        exp: 104,
    };
    assert_eq!(x.into_float::<f32>(), f32::INFINITY);

    // max value + 1
    let x = ExtendedFloat {
        mant: 1048576,
        exp: 108,
    };
    assert_eq!(x.into_float::<f32>(), f32::INFINITY);

    // 1e40
    let x = ExtendedFloat {
        mant: 16940658945086007296,
        exp: 69,
    };
    assert_eq!(x.into_float::<f32>(), f32::INFINITY);

    // Integers.
    for int in &INTEGERS {
        let fp = ExtendedFloat { mant: *int, exp: 0 };
        assert_eq!(fp.into_float::<f32>(), *int as f32, "{:?} as f32", *int);
    }
}

#[test]
fn to_f64_test() {
    // underflow
    let x = ExtendedFloat {
        mant: 9223372036854775808,
        exp: -1138,
    };
    assert_eq!(x.into_float::<f64>(), 0.0);

    // min value
    let x = ExtendedFloat {
        mant: 9223372036854775808,
        exp: -1137,
    };
    assert_eq!(x.into_float::<f64>(), 5e-324);

    // 1.0e-250
    let x = ExtendedFloat {
        mant: 13207363278391631872,
        exp: -894,
    };
    assert_eq!(x.into_float::<f64>(), 1e-250);

    // 1.0e-150
    let x = ExtendedFloat {
        mant: 15095849699286165504,
        exp: -562,
    };
    assert_eq!(x.into_float::<f64>(), 1e-150);

    // 1.0e-45
    let x = ExtendedFloat {
        mant: 13164036458569648128,
        exp: -213,
    };
    assert_eq!(x.into_float::<f64>(), 1e-45);

    // 1.0e-40
    let x = ExtendedFloat {
        mant: 10043362776618688512,
        exp: -196,
    };
    assert_eq!(x.into_float::<f64>(), 1e-40);

    // 1.0e-20
    let x = ExtendedFloat {
        mant: 13611294676837537792,
        exp: -130,
    };
    assert_eq!(x.into_float::<f64>(), 1e-20);

    // 1.0
    let x = ExtendedFloat {
        mant: 9223372036854775808,
        exp: -63,
    };
    assert_eq!(x.into_float::<f64>(), 1.0);

    // 1e20
    let x = ExtendedFloat {
        mant: 12500000000000000000,
        exp: 3,
    };
    assert_eq!(x.into_float::<f64>(), 1e20);

    // 1e40
    let x = ExtendedFloat {
        mant: 16940658945086007296,
        exp: 69,
    };
    assert_eq!(x.into_float::<f64>(), 1e40);

    // 1e150
    let x = ExtendedFloat {
        mant: 11270725851789228032,
        exp: 435,
    };
    assert_eq!(x.into_float::<f64>(), 1e150);

    // 1e250
    let x = ExtendedFloat {
        mant: 12882297539194265600,
        exp: 767,
    };
    assert_eq!(x.into_float::<f64>(), 1e250);

    // max value
    let x = ExtendedFloat {
        mant: 9007199254740991,
        exp: 971,
    };
    assert_eq!(x.into_float::<f64>(), 1.7976931348623157e308);

    // max value
    let x = ExtendedFloat {
        mant: 18446744073709549568,
        exp: 960,
    };
    assert_eq!(x.into_float::<f64>(), 1.7976931348623157e308);

    // overflow
    let x = ExtendedFloat {
        mant: 9007199254740992,
        exp: 971,
    };
    assert_eq!(x.into_float::<f64>(), f64::INFINITY);

    // overflow
    let x = ExtendedFloat {
        mant: 18446744073709549568,
        exp: 961,
    };
    assert_eq!(x.into_float::<f64>(), f64::INFINITY);

    // Underflow
    // Adapted from failures in strtod.
    let x = ExtendedFloat {
        exp: -1139,
        mant: 18446744073709550712,
    };
    assert_eq!(x.into_float::<f64>(), 0.0);

    let x = ExtendedFloat {
        exp: -1139,
        mant: 18446744073709551460,
    };
    assert_eq!(x.into_float::<f64>(), 0.0);

    let x = ExtendedFloat {
        exp: -1138,
        mant: 9223372036854776103,
    };
    assert_eq!(x.into_float::<f64>(), 5e-324);

    // Integers.
    for int in &INTEGERS {
        let fp = ExtendedFloat { mant: *int, exp: 0 };
        assert_eq!(fp.into_float::<f64>(), *int as f64, "{:?} as f64", *int);
    }
}

// OPERATIONS

fn check_mul(a: ExtendedFloat, b: ExtendedFloat, c: ExtendedFloat) {
    let r = a.mul(&b);
    assert_eq!(r, c);
}

#[test]
fn mul_test() {
    // Normalized (64-bit mantissa)
    let a = ExtendedFloat {
        mant: 13164036458569648128,
        exp: -213,
    };
    let b = ExtendedFloat {
        mant: 9223372036854775808,
        exp: -62,
    };
    let c = ExtendedFloat {
        mant: 6582018229284824064,
        exp: -211,
    };
    check_mul(a, b, c);

    // Check with integers
    // 64-bit mantissa
    let mut a = ExtendedFloat { mant: 10, exp: 0 };
    let mut b = ExtendedFloat { mant: 10, exp: 0 };
    a.normalize();
    b.normalize();
    assert_eq!(a.mul(&b).into_float::<f64>(), 100.0);

    // Check both values need high bits set.
    let a = ExtendedFloat {
        mant: 1 << 32,
        exp: -31,
    };
    let b = ExtendedFloat {
        mant: 1 << 32,
        exp: -31,
    };
    assert_eq!(a.mul(&b).into_float::<f64>(), 4.0);

    // Check both values need high bits set.
    let a = ExtendedFloat {
        mant: 10 << 31,
        exp: -31,
    };
    let b = ExtendedFloat {
        mant: 10 << 31,
        exp: -31,
    };
    assert_eq!(a.mul(&b).into_float::<f64>(), 100.0);
}

fn check_imul(mut a: ExtendedFloat, b: ExtendedFloat, c: ExtendedFloat) {
    a.imul(&b);
    assert_eq!(a, c);
}

#[test]
fn imul_test() {
    // Normalized (64-bit mantissa)
    let a = ExtendedFloat {
        mant: 13164036458569648128,
        exp: -213,
    };
    let b = ExtendedFloat {
        mant: 9223372036854775808,
        exp: -62,
    };
    let c = ExtendedFloat {
        mant: 6582018229284824064,
        exp: -211,
    };
    check_imul(a, b, c);

    // Check with integers
    // 64-bit mantissa
    let mut a = ExtendedFloat { mant: 10, exp: 0 };
    let mut b = ExtendedFloat { mant: 10, exp: 0 };
    a.normalize();
    b.normalize();
    a.imul(&b);
    assert_eq!(a.into_float::<f64>(), 100.0);

    // Check both values need high bits set.
    let mut a = ExtendedFloat {
        mant: 1 << 32,
        exp: -31,
    };
    let b = ExtendedFloat {
        mant: 1 << 32,
        exp: -31,
    };
    a.imul(&b);
    assert_eq!(a.into_float::<f64>(), 4.0);

    // Check both values need high bits set.
    let mut a = ExtendedFloat {
        mant: 10 << 31,
        exp: -31,
    };
    let b = ExtendedFloat {
        mant: 10 << 31,
        exp: -31,
    };
    a.imul(&b);
    assert_eq!(a.into_float::<f64>(), 100.0);
}

[Dauer der Verarbeitung: 0.28 Sekunden, vorverarbeitet 2026-06-06]