Skip to content

StrToF64

Description

Convert string to double

Parameters

Name Direction Description
str in String to convert
value out Where to store the result
config in Parse configuration (NULL for default parsing)

Success

Returns true and writes the parsed value into *value

Failure

Returns false when the input cannot be parsed; *value is left unchanged

Usage example (Cross-references)

Usage examples (Cross-references)
            // generic Str-to-float path; non-matching tokens fall back to
            // the digit-by-digit scan below.
            if (StrToF64(&temp, v, NULL)) {
                StrDeinit(&temp);
                DefaultAllocatorDeinit(&scratch);
        }
    
        if (!StrToF64(&temp, v, NULL)) {
            LOG_ERROR("Failed to parse f64");
            StrDeinit(&temp);
            // the digit-by-digit scan below.
            f64 val;
            if (StrToF64(&temp, &val, NULL)) {
                *v = (f32)val;
                StrDeinit(&temp);
        // f32 parser would duplicate the entire StrToF64 logic for no gain.
        f64 val;
        if (!StrToF64(&temp, &val, NULL)) {
            LOG_ERROR("Failed to parse f32");
            StrDeinit(&temp);
    }
    
    bool StrToF64(const Str *str, f64 *value, const StrParseConfig *config) {
        ValidateStr(str);
    // Test StrToF64 function
    bool test_str_to_f64(void) {
        WriteFmt("Testing StrToF64\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str  s       = StrInitFromZstr("123", &alloc);
        f64  value   = 0.0;
        bool success = StrToF64(&s, &value, NULL);
        bool result  = (success && F64Abs(value - 123.0) < 0.0001);
        StrDeinit(&s);
        s       = StrInitFromZstr("123.456", &alloc);
        success = StrToF64(&s, &value, NULL);
        result  = result && (success && F64Abs(value - 123.456) < 0.0001);
        StrDeinit(&s);
        s       = StrInitFromZstr("-123.456", &alloc);
        success = StrToF64(&s, &value, NULL);
        result  = result && (success && F64Abs(value - (-123.456)) < 0.0001);
        StrDeinit(&s);
        s       = StrInitFromZstr("1.23e2", &alloc);
        success = StrToF64(&s, &value, NULL);
        result  = result && (success && F64Abs(value - 123.0) < 0.0001);
        StrDeinit(&s);
        s       = StrInitFromZstr("0", &alloc);
        success = StrToF64(&s, &value, NULL);
        result  = result && (success && F64Abs(value) < 0.0001);
        StrDeinit(&s);
        s       = StrInitFromZstr("inf", &alloc);
        success = StrToF64(&s, &value, NULL);
        result  = result && (success && F64IsInf(value) && value > 0);
        StrDeinit(&s);
        s       = StrInitFromZstr("-inf", &alloc);
        success = StrToF64(&s, &value, NULL);
        result  = result && (success && F64IsInf(value) && value < 0);
        StrDeinit(&s);
        s       = StrInitFromZstr("nan", &alloc);
        success = StrToF64(&s, &value, NULL);
        result  = result && (success && F64IsNan(value));
        StrDeinit(&s);
        s       = StrInitFromZstr("not a number", &alloc);
        success = StrToF64(&s, &value, NULL);
        result  = result && (!success);
                StrFromF64(&s, f64_values[i], &config);
                f64  recovered_f64 = 0.0;
                bool success       = StrToF64(&s, &recovered_f64, NULL);
    
                // Allow for precision loss
        StrFromF64(&s, 1e-300, &fconfig);
        f64 recovered_small = 0.0;
        success             = StrToF64(&s, &recovered_small, NULL);
        result              = result && success && (recovered_small < 1e-299);
        StrFromF64(&s, 1e300, &fconfig);
        f64 recovered_large = 0.0;
        success             = StrToF64(&s, &recovered_large, NULL);
        result              = result && success && (recovered_large > 1e299);
    
                f64  recovered = 0.0;
                bool success   = StrToF64(&s, &recovered, NULL);
    
                // Allow for floating point precision issues
Last updated on