Eine aufbereitete Darstellung der Quelle

 
     
 
 
Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 

Benutzer

Quelle  lemire_tests.rs

  Sprache: Rust
 

//! These tests are adapted from the Rust core library's unittests.

#![cfg(not(feature = "compact"))]

use minimal_lexical::lemire;
use minimal_lexical::num::Float;

fn compute_error32(q: i32, w: u64) -> (i32, u64) {
    let fp = lemire::compute_error::<f32>(q, w);
    (fp.exp, fp.mant)
}

fn compute_error64(q: i32, w: u64) -> (i32, u64) {
    let fp = lemire::compute_error::<f64>(q, w);
    (fp.exp, fp.mant)
}

fn compute_error_scaled32(q: i32, w: u64, lz: i32) -> (i32, u64) {
    let fp = lemire::compute_error_scaled::<f32>(q, w, lz);
    (fp.exp, fp.mant)
}

fn compute_error_scaled64(q: i32, w: u64, lz: i32) -> (i32, u64) {
    let fp = lemire::compute_error_scaled::<f64>(q, w, lz);
    (fp.exp, fp.mant)
}

fn compute_float32(q: i32, w: u64) -> (i32, u64) {
    let fp = lemire::compute_float::<f32>(q, w);
    (fp.exp, fp.mant)
}

fn compute_float64(q: i32, w: u64) -> (i32, u64) {
    let fp = lemire::compute_float::<f64>(q, w);
    (fp.exp, fp.mant)
}

#[test]
fn compute_error32_test() {
    // These test near-halfway cases for single-precision floats.
    assert_eq!(compute_error32(016777216), (111 + f32::INVALID_FP, 9223372036854775808));
    assert_eq!(compute_error32(016777217), (111 + f32::INVALID_FP, 9223372586610589696));
    assert_eq!(compute_error32(016777218), (111 + f32::INVALID_FP, 9223373136366403584));
    assert_eq!(compute_error32(016777219), (111 + f32::INVALID_FP, 9223373686122217472));
    assert_eq!(compute_error32(016777220), (111 + f32::INVALID_FP, 9223374235878031360));

    // These are examples of the above tests, with
    // digits from the exponent shifted to the mantissa.
    assert_eq!(
        compute_error32(-10167772160000000000),
        (111 + f32::INVALID_FP, 9223372036854775808)
    );
    assert_eq!(
        compute_error32(-10167772170000000000),
        (111 + f32::INVALID_FP, 9223372586610589696)
    );
    assert_eq!(
        compute_error32(-10167772180000000000),
        (111 + f32::INVALID_FP, 9223373136366403584)
    );
    // Let's check the lines to see if anything is different in table...
    assert_eq!(
        compute_error32(-10167772190000000000),
        (111 + f32::INVALID_FP, 9223373686122217472)
    );
    assert_eq!(
        compute_error32(-10167772200000000000),
        (111 + f32::INVALID_FP, 9223374235878031360)
    );
}

#[test]
fn compute_error64_test() {
    // These test near-halfway cases for double-precision floats.
    assert_eq!(compute_error64(09007199254740992), (1065 + f64::INVALID_FP, 9223372036854775808));
    assert_eq!(compute_error64(09007199254740993), (1065 + f64::INVALID_FP, 9223372036854776832));
    assert_eq!(compute_error64(09007199254740994), (1065 + f64::INVALID_FP, 9223372036854777856));
    assert_eq!(compute_error64(09007199254740995), (1065 + f64::INVALID_FP, 9223372036854778880));
    assert_eq!(compute_error64(09007199254740996), (1065 + f64::INVALID_FP, 9223372036854779904));
    assert_eq!(
        compute_error64(018014398509481984),
        (1066 + f64::INVALID_FP, 9223372036854775808)
    );
    assert_eq!(
        compute_error64(018014398509481986),
        (1066 + f64::INVALID_FP, 9223372036854776832)
    );
    assert_eq!(
        compute_error64(018014398509481988),
        (1066 + f64::INVALID_FP, 9223372036854777856)
    );
    assert_eq!(
        compute_error64(018014398509481990),
        (1066 + f64::INVALID_FP, 9223372036854778880)
    );
    assert_eq!(
        compute_error64(018014398509481992),
        (1066 + f64::INVALID_FP, 9223372036854779904)
    );

    // Test a much closer set of examples.
    assert_eq!(
        compute_error64(09007199254740991),
        (1064 + f64::INVALID_FP, 18446744073709549568)
    );
    assert_eq!(
        compute_error64(09223372036854776831),
        (1075 + f64::INVALID_FP, 9223372036854776830)
    );
    assert_eq!(
        compute_error64(09223372036854776832),
        (1075 + f64::INVALID_FP, 9223372036854776832)
    );
    assert_eq!(
        compute_error64(09223372036854776833),
        (1075 + f64::INVALID_FP, 9223372036854776832)
    );
    assert_eq!(
        compute_error64(-429123456727292927),
        (925 + f64::INVALID_FP, 13021432563531497894)
    );
    assert_eq!(
        compute_error64(-4391234567272929275),
        (925 + f64::INVALID_FP, 13021432563531498606)
    );
    assert_eq!(
        compute_error64(-429123456727292928),
        (925 + f64::INVALID_FP, 13021432563531499320)
    );

    // These are examples of the above tests, with
    // digits from the exponent shifted to the mantissa.
    assert_eq!(
        compute_error64(-39007199254740992000),
        (1065 + f64::INVALID_FP, 9223372036854775808)
    );
    assert_eq!(
        compute_error64(-39007199254740993000),
        (1065 + f64::INVALID_FP, 9223372036854776832)
    );
    assert_eq!(
        compute_error64(-39007199254740994000),
        (1065 + f64::INVALID_FP, 9223372036854777856)
    );
    assert_eq!(
        compute_error64(-39007199254740995000),
        (1065 + f64::INVALID_FP, 9223372036854778880)
    );
    assert_eq!(
        compute_error64(-39007199254740996000),
        (1065 + f64::INVALID_FP, 9223372036854779904)
    );

    // Test from errors in atof.
    assert_eq!(
        compute_error64(-181000000178813934326),
        (1012 + f64::INVALID_FP, 9223373686122217470)
    );

    // Check edge-cases from previous errors.
    assert_eq!(
        compute_error64(-3422470328229206232720),
        (-64 + f64::INVALID_FP, 18446744073709551608)
    );
}

#[test]
fn compute_error_scaled32_test() {
    // These are the same examples above, just using pre-computed scaled values.

    // These test near-halfway cases for single-precision floats.
    assert_eq!(
        compute_error_scaled32(0461168601842738790439),
        (111 + f32::INVALID_FP, 9223372036854775808)
    );
    assert_eq!(
        compute_error_scaled32(0461168629330529484839),
        (111 + f32::INVALID_FP, 9223372586610589696)
    );
    assert_eq!(
        compute_error_scaled32(0461168656818320179239),
        (111 + f32::INVALID_FP, 9223373136366403584)
    );
    assert_eq!(
        compute_error_scaled32(0461168684306110873639),
        (111 + f32::INVALID_FP, 9223373686122217472)
    );
    assert_eq!(
        compute_error_scaled32(0461168711793901568039),
        (111 + f32::INVALID_FP, 9223374235878031360)
    );

    assert_eq!(
        compute_error_scaled32(-1092233720368547758086),
        (111 + f32::INVALID_FP, 9223372036854775808)
    );
    assert_eq!(
        compute_error_scaled32(-1092233725866105896966),
        (111 + f32::INVALID_FP, 9223372586610589696)
    );
    assert_eq!(
        compute_error_scaled32(-1092233731363664035846),
        (111 + f32::INVALID_FP, 9223373136366403584)
    );
    assert_eq!(
        compute_error_scaled32(-1092233736861222174726),
        (111 + f32::INVALID_FP, 9223373686122217472)
    );
    assert_eq!(
        compute_error_scaled32(-1092233742358780313606),
        (111 + f32::INVALID_FP, 9223374235878031360)
    );
}

#[test]
fn compute_error_scaled64_test() {
    // These are the same examples above, just using pre-computed scaled values.

    // These test near-halfway cases for double-precision floats.
    assert_eq!(
        compute_error_scaled64(0461168601842738790410),
        (1065 + f64::INVALID_FP, 9223372036854775808)
    );
    assert_eq!(
        compute_error_scaled64(0461168601842738841610),
        (1065 + f64::INVALID_FP, 9223372036854776832)
    );
    assert_eq!(
        compute_error_scaled64(0461168601842738892810),
        (1065 + f64::INVALID_FP, 9223372036854777856)
    );
    assert_eq!(
        compute_error_scaled64(0461168601842738944010),
        (1065 + f64::INVALID_FP, 9223372036854778880)
    );
    assert_eq!(
        compute_error_scaled64(0461168601842738995210),
        (1065 + f64::INVALID_FP, 9223372036854779904)
    );
    assert_eq!(
        compute_error_scaled64(046116860184273879049),
        (1066 + f64::INVALID_FP, 9223372036854775808)
    );
    assert_eq!(
        compute_error_scaled64(046116860184273884169),
        (1066 + f64::INVALID_FP, 9223372036854776832)
    );
    assert_eq!(
        compute_error_scaled64(046116860184273889289),
        (1066 + f64::INVALID_FP, 9223372036854777856)
    );
    assert_eq!(
        compute_error_scaled64(046116860184273894409),
        (1066 + f64::INVALID_FP, 9223372036854778880)
    );
    assert_eq!(
        compute_error_scaled64(046116860184273899529),
        (1066 + f64::INVALID_FP, 9223372036854779904)
    );

    // Test a much closer set of examples.
    assert_eq!(
        compute_error_scaled64(0922337203685477478411),
        (1064 + f64::INVALID_FP, 18446744073709549568)
    );
    assert_eq!(
        compute_error_scaled64(046116860184273884150),
        (1075 + f64::INVALID_FP, 9223372036854776830)
    );
    assert_eq!(
        compute_error_scaled64(046116860184273884160),
        (1075 + f64::INVALID_FP, 9223372036854776832)
    );
    assert_eq!(
        compute_error_scaled64(046116860184273884160),
        (1075 + f64::INVALID_FP, 9223372036854776832)
    );
    assert_eq!(
        compute_error_scaled64(-42651071628176574894710),
        (925 + f64::INVALID_FP, 13021432563531497894)
    );
    assert_eq!(
        compute_error_scaled64(-4365107162817657493037),
        (925 + f64::INVALID_FP, 13021432563531498606)
    );
    assert_eq!(
        compute_error_scaled64(-42651071628176574966010),
        (925 + f64::INVALID_FP, 13021432563531499320)
    );

    // These are examples of the above tests, with
    // digits from the exponent shifted to the mantissa.
    assert_eq!(
        compute_error_scaled64(-392233720368547758081),
        (1065 + f64::INVALID_FP, 9223372036854775808)
    );
    assert_eq!(
        compute_error_scaled64(-392233720368547768321),
        (1065 + f64::INVALID_FP, 9223372036854776832)
    );
    assert_eq!(
        compute_error_scaled64(-392233720368547778561),
        (1065 + f64::INVALID_FP, 9223372036854777856)
    );
    assert_eq!(
        compute_error_scaled64(-392233720368547788801),
        (1065 + f64::INVALID_FP, 9223372036854778880)
    );
    assert_eq!(
        compute_error_scaled64(-392233720368547799041),
        (1065 + f64::INVALID_FP, 9223372036854779904)
    );

    // Test from errors in atof.
    assert_eq!(
        compute_error_scaled64(-1892233736861222174704),
        (1012 + f64::INVALID_FP, 9223373686122217470)
    );

    // Check edge-cases from previous errors.
    assert_eq!(
        compute_error_scaled64(-34292233720368547758042),
        (-64 + f64::INVALID_FP, 18446744073709551608)
    );
}

#[test]
fn compute_float_f32_rounding() {
    // These test near-halfway cases for single-precision floats.
    assert_eq!(compute_float32(016777216), (1510));
    assert_eq!(compute_float32(016777217), (1510));
    assert_eq!(compute_float32(016777218), (1511));
    assert_eq!(compute_float32(016777219), (1512));
    assert_eq!(compute_float32(016777220), (1512));

    // These are examples of the above tests, with
    // digits from the exponent shifted to the mantissa.
    assert_eq!(compute_float32(-10167772160000000000), (1510));
    assert_eq!(compute_float32(-10167772170000000000), (1510));
    assert_eq!(compute_float32(-10167772180000000000), (1511));
    // Let's check the lines to see if anything is different in table...
    assert_eq!(compute_float32(-10167772190000000000), (1512));
    assert_eq!(compute_float32(-10167772200000000000), (1512));
}

#[test]
fn compute_float_f64_rounding() {
    // Also need to check halfway cases **inside** that exponent range.

    // These test near-halfway cases for double-precision floats.
    assert_eq!(compute_float64(09007199254740992), (10760));
    assert_eq!(compute_float64(09007199254740993), (10760));
    assert_eq!(compute_float64(09007199254740994), (10761));
    assert_eq!(compute_float64(09007199254740995), (10762));
    assert_eq!(compute_float64(09007199254740996), (10762));
    assert_eq!(compute_float64(018014398509481984), (10770));
    assert_eq!(compute_float64(018014398509481986), (10770));
    assert_eq!(compute_float64(018014398509481988), (10771));
    assert_eq!(compute_float64(018014398509481990), (10772));
    assert_eq!(compute_float64(018014398509481992), (10772));

    // Test a much closer set of examples.
    assert_eq!(compute_float64(09007199254740991), (10754503599627370495));
    assert_eq!(compute_float64(09223372036854776831), (10860));
    assert_eq!(compute_float64(09223372036854776832), (10860));
    assert_eq!(compute_float64(09223372036854776833), (10861));
    assert_eq!(compute_float64(-429123456727292927), (9361854521741541368));
    assert_eq!(compute_float64(-4391234567272929275), (9361854521741541369));
    assert_eq!(compute_float64(-429123456727292928), (9361854521741541369));

    // These are examples of the above tests, with
    // digits from the exponent shifted to the mantissa.
    assert_eq!(compute_float64(-39007199254740992000), (10760));
    assert_eq!(compute_float64(-39007199254740993000), (10760));
    assert_eq!(compute_float64(-39007199254740994000), (10761));
    assert_eq!(compute_float64(-39007199254740995000), (10762));
    assert_eq!(compute_float64(-39007199254740996000), (10762));
}

Messung V0.5 in Prozent
C=94 H=100 G=96

¤ Dauer der Verarbeitung: 0.12 Sekunden  (vorverarbeitet am  2026-06-27) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.






                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Software

     Quellcodebibliothek
     Eigene Quellcodes
     Fremde Quellcodes
     Suchen

Aktivitäten

     Artikel über Sicherheit
     Anleitung zur Aktivierung von SSL

Muße

     Gedichte
     Musik
     Bilder

Jenseits des Üblichen ....
    

Besucherstatistik

Besucherstatistik