Skip to content

FloatFromStr

FloatFromStr

Description

Parse a decimal string into a float. Supports an optional sign, decimal point, and scientific exponent.

Aborts on malformed input.

Parameters

Name Direction Description
text in Input string

Usage example (from documentation)

  Float value = FloatFromStr("-1.25e6");

Returns

Parsed floating-point value.

Usage example (Cross-references)

Usage examples (Cross-references)
    
        temp   = StrInitFromCstr(start, token_len);
        parsed = FloatFromStr(temp.data);
    
        FloatDeinit(value);
        }
    
        return FloatFromStr(text);
    }
        }
    
        return FloatFromStr(text);
    }
    }
    
    Float FloatFromStr(const char *text) {
        Float result          = FloatInit();
        Str   digits          = StrInit();
        WriteFmt("Testing FloatNegate and FloatAbs\n");
    
        Float value = FloatFromStr("12.5");
        Str   text  = StrInit();
        WriteFmt("Testing FloatAdd with small floats\n");
    
        Float a            = FloatFromStr("1.2");
        Float b            = FloatFromStr("0.03");
        Float result_value = FloatInit();
    
        Float a            = FloatFromStr("1.2");
        Float b            = FloatFromStr("0.03");
        Float result_value = FloatInit();
        Str   text         = StrInit();
        WriteFmt("Testing FloatAdd with very large floats\n");
    
        Float a            = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
        Float b            = FloatFromStr(FLOAT_TEST_VERY_LARGE_TWOS);
        Float result_value = FloatInit();
    
        Float a            = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
        Float b            = FloatFromStr(FLOAT_TEST_VERY_LARGE_TWOS);
        Float result_value = FloatInit();
        Str   text         = StrInit();
        WriteFmt("Testing FloatAdd generic dispatch\n");
    
        Float a            = FloatFromStr("1.25");
        Float b            = FloatFromStr("0.75");
        Int   whole        = IntFrom(2);
    
        Float a            = FloatFromStr("1.25");
        Float b            = FloatFromStr("0.75");
        Int   whole        = IntFrom(2);
        Float result_value = FloatInit();
        WriteFmt("Testing FloatSub with small floats\n");
    
        Float a            = FloatFromStr("1.5");
        Float b            = FloatFromStr("2");
        Float result_value = FloatInit();
    
        Float a            = FloatFromStr("1.5");
        Float b            = FloatFromStr("2");
        Float result_value = FloatInit();
        Str   text         = StrInit();
        WriteFmt("Testing FloatSub with very large floats\n");
    
        Float a            = FloatFromStr(FLOAT_TEST_VERY_LARGE_THREES);
        Float b            = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
        Float result_value = FloatInit();
    
        Float a            = FloatFromStr(FLOAT_TEST_VERY_LARGE_THREES);
        Float b            = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
        Float result_value = FloatInit();
        Str   text         = StrInit();
        WriteFmt("Testing FloatSub generic dispatch\n");
    
        Float a            = FloatFromStr("5.5");
        Float b            = FloatFromStr("0.5");
        Int   whole        = IntFrom(2);
    
        Float a            = FloatFromStr("5.5");
        Float b            = FloatFromStr("0.5");
        Int   whole        = IntFrom(2);
        Float result_value = FloatInit();
        WriteFmt("Testing FloatMul with small floats\n");
    
        Float a            = FloatFromStr("12.5");
        Float b            = FloatFromStr("-0.2");
        Float result_value = FloatInit();
    
        Float a            = FloatFromStr("12.5");
        Float b            = FloatFromStr("-0.2");
        Float result_value = FloatInit();
        Str   text         = StrInit();
        WriteFmt("Testing FloatMul with very large and small floats\n");
    
        Float a            = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
        Float b            = FloatFromStr("2");
        Float result_value = FloatInit();
    
        Float a            = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
        Float b            = FloatFromStr("2");
        Float result_value = FloatInit();
        Str   text         = StrInit();
        WriteFmt("Testing FloatMul generic dispatch\n");
    
        Float a            = FloatFromStr("1.5");
        Float b            = FloatFromStr("2");
        Int   whole        = IntFrom(2);
    
        Float a            = FloatFromStr("1.5");
        Float b            = FloatFromStr("2");
        Int   whole        = IntFrom(2);
        Float result_value = FloatInit();
        WriteFmt("Testing FloatDiv with small floats\n");
    
        Float a            = FloatFromStr("1");
        Float b            = FloatFromStr("8");
        Float result_value = FloatInit();
    
        Float a            = FloatFromStr("1");
        Float b            = FloatFromStr("8");
        Float result_value = FloatInit();
        Str   text         = StrInit();
        WriteFmt("Testing FloatDiv with very large and small floats\n");
    
        Float a            = FloatFromStr(FLOAT_TEST_VERY_LARGE_TWOS);
        Float b            = FloatFromStr("2");
        Float result_value = FloatInit();
    
        Float a            = FloatFromStr(FLOAT_TEST_VERY_LARGE_TWOS);
        Float b            = FloatFromStr("2");
        Float result_value = FloatInit();
        Str   text         = StrInit();
        WriteFmt("Testing FloatDiv generic dispatch\n");
    
        Float a            = FloatFromStr("7.5");
        Float b            = FloatFromStr("2.5");
        Int   whole        = IntFrom(3);
    
        Float a            = FloatFromStr("7.5");
        Float b            = FloatFromStr("2.5");
        Int   whole        = IntFrom(3);
        Float result_value = FloatInit();
        WriteFmt("Testing FloatDiv divide-by-zero handling\n");
    
        Float a = FloatFromStr("1");
        Float b = FloatInit();
        Float r = FloatInit();
        WriteFmt("Testing FloatCompare with small floats\n");
    
        Float a = FloatFromStr("1.23");
        Float b = FloatFromStr("123e-2");
        Float c = FloatFromStr("-1.23");
    
        Float a = FloatFromStr("1.23");
        Float b = FloatFromStr("123e-2");
        Float c = FloatFromStr("-1.23");
        Float a = FloatFromStr("1.23");
        Float b = FloatFromStr("123e-2");
        Float c = FloatFromStr("-1.23");
    
        bool result = FloatCompare(&a, &b) == 0;
        WriteFmt("Testing FloatCompare with very large floats\n");
    
        Float a = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
        Float b = FloatFromStr(FLOAT_TEST_VERY_LARGE_TWOS);
        Float c = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
    
        Float a = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
        Float b = FloatFromStr(FLOAT_TEST_VERY_LARGE_TWOS);
        Float c = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
        Float a = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
        Float b = FloatFromStr(FLOAT_TEST_VERY_LARGE_TWOS);
        Float c = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
    
        bool result = FloatLT(&a, &b);
        WriteFmt("Testing FloatCompare with very large and small floats\n");
    
        Float large          = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
        Float negative_large = FloatFromStr("-" FLOAT_TEST_VERY_LARGE_ONES);
        Float small          = FloatFromStr("2.5");
    
        Float large          = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
        Float negative_large = FloatFromStr("-" FLOAT_TEST_VERY_LARGE_ONES);
        Float small          = FloatFromStr("2.5");
        Float large          = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
        Float negative_large = FloatFromStr("-" FLOAT_TEST_VERY_LARGE_ONES);
        Float small          = FloatFromStr("2.5");
    
        bool result = FloatGT(&large, &small);
        WriteFmt("Testing Float compare macros\n");
    
        Float a        = FloatFromStr("-2");
        Float b        = FloatFromStr("0.5");
        Float expected = FloatFromStr("5e-1");
    
        Float a        = FloatFromStr("-2");
        Float b        = FloatFromStr("0.5");
        Float expected = FloatFromStr("5e-1");
        Float a        = FloatFromStr("-2");
        Float b        = FloatFromStr("0.5");
        Float expected = FloatFromStr("5e-1");
    
        bool result = FloatLT(&a, &b);
        WriteFmt("Testing FloatCompare generic dispatch\n");
    
        Float value = FloatFromStr("12.5");
        Float same  = FloatFromStr("12.5");
        Int   whole = IntFrom(12);
    
        Float value = FloatFromStr("12.5");
        Float same  = FloatFromStr("12.5");
        Int   whole = IntFrom(12);
        Int   next  = IntFrom(13);
        Str   output  = StrInit();
        bool  success = true;
        Float exact   = FloatFromStr("1234567890.012345");
        Float sci     = FloatFromStr("12345.67");
        Float short_v = FloatFromStr("1.2");
        bool  success = true;
        Float exact   = FloatFromStr("1234567890.012345");
        Float sci     = FloatFromStr("12345.67");
        Float short_v = FloatFromStr("1.2");
        Float exact   = FloatFromStr("1234567890.012345");
        Float sci     = FloatFromStr("12345.67");
        Float short_v = FloatFromStr("1.2");
    
        StrWriteFmt(&output, "{}", exact);
        WriteFmt("Testing FloatToInt exact conversion\n");
    
        Float value        = FloatFromStr("1234500e-2");
        Int   result_value = IntInit();
        Str   text         = StrInit();
        WriteFmt("Testing FloatToInt fractional failure handling\n");
    
        Float value        = FloatFromStr("123.45");
        Int   result_value = IntFrom(99);
        WriteFmt("Testing FloatToInt negative failure handling\n");
    
        Float value        = FloatFromStr("-42");
        Int   result_value = IntFrom(99);
        WriteFmt("Testing Float string round trip\n");
    
        Float value = FloatFromStr("-123.45");
        Str   text  = FloatToStr(&value);
        WriteFmt("Testing Float very large string round trip\n");
    
        Float value = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
        Str   text  = FloatToStr(&value);
        WriteFmt("Testing Float scientific parsing\n");
    
        Float value = FloatFromStr("1.2300e3");
        Str   text  = FloatToStr(&value);
    
    bool test_float_from_str_invalid(void) {
        WriteFmt("Testing FloatFromStr invalid format handling\n");
    
        FloatFromStr("12.3.4");
        WriteFmt("Testing FloatFromStr invalid format handling\n");
    
        FloatFromStr("12.3.4");
        return false;
    }
    
    bool test_float_from_str_null(void) {
        WriteFmt("Testing FloatFromStr NULL handling\n");
    
        FloatFromStr(NULL);
        WriteFmt("Testing FloatFromStr NULL handling\n");
    
        FloatFromStr(NULL);
        return false;
    }
    
        Float zero  = FloatInit();
        Float value = FloatFromStr("0.001");
    
        bool result = FloatIsZero(&zero);
        WriteFmt("Testing FloatIsNegative\n");
    
        Float neg  = FloatFromStr("-42");
        Float pos  = FloatFromStr("42");
        Float zero = FloatFromStr("-0.0");
    
        Float neg  = FloatFromStr("-42");
        Float pos  = FloatFromStr("42");
        Float zero = FloatFromStr("-0.0");
        Float neg  = FloatFromStr("-42");
        Float pos  = FloatFromStr("42");
        Float zero = FloatFromStr("-0.0");
    
        bool result = FloatIsNegative(&neg);
        WriteFmt("Testing FloatExponent\n");
    
        Float value = FloatFromStr("12.34");
    
        bool result = FloatExponent(&value) == -2;
        WriteFmt("Testing FloatClear\n");
    
        Float value = FloatFromStr("-123.45");
    
        FloatClear(&value);
        WriteFmt("Testing FloatClone\n");
    
        Float original = FloatFromStr("-12.5");
        Float clone    = FloatClone(&original);
        Float expected = FloatFromStr("-12.5");
        Float original = FloatFromStr("-12.5");
        Float clone    = FloatClone(&original);
        Float expected = FloatFromStr("-12.5");
        Str   text     = FloatToStr(&clone);
Last updated on