StrToF64
- Function
- August 22, 2025
Table of Contents
StrToF64
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 stores result in value
Failure
Returns false if conversion fails
Usage example (Cross-references)
- In
Io.c:1546
:
// Try to parse as special value
if (StrToF64(&temp, v, NULL)) {
StrDeinit(&temp);
return i;
- In
Io.c:1611
:
// Use StrToF64 directly
if (!StrToF64(&temp, v, NULL)) {
LOG_ERROR("Failed to parse f64");
StrDeinit(&temp);
- In
Io.c:2444
:
// Try to parse as special value
f64 val;
if (StrToF64(&temp, &val, NULL)) {
*v = (f32)val;
StrDeinit(&temp);
- In
Io.c:2511
:
// Use StrToF64 directly
f64 val;
if (!StrToF64(&temp, &val, NULL)) {
LOG_ERROR("Failed to parse f32");
StrDeinit(&temp);
- In
Str.c:721
:
}
bool StrToF64(const Str* str, f64* value, const StrParseConfig* config) {
ValidateStr(str);
// Test StrToF64 function
bool test_str_to_f64(void) {
printf("Testing StrToF64\n");
// Test integer conversion
Str s = StrInitFromZstr("123");
f64 value = 0.0;
bool success = StrToF64(&s, &value, NULL);
bool result = (success && fabs(value - 123.0) < 0.0001);
StrDeinit(&s);
s = StrInitFromZstr("123.456");
success = StrToF64(&s, &value, NULL);
result = result && (success && fabs(value - 123.456) < 0.0001);
StrDeinit(&s);
s = StrInitFromZstr("-123.456");
success = StrToF64(&s, &value, NULL);
result = result && (success && fabs(value - (-123.456)) < 0.0001);
StrDeinit(&s);
s = StrInitFromZstr("1.23e2");
success = StrToF64(&s, &value, NULL);
result = result && (success && fabs(value - 123.0) < 0.0001);
StrDeinit(&s);
s = StrInitFromZstr("0");
success = StrToF64(&s, &value, NULL);
result = result && (success && fabs(value) < 0.0001);
StrDeinit(&s);
s = StrInitFromZstr("inf");
success = StrToF64(&s, &value, NULL);
result = result && (success && isinf(value) && value > 0);
StrDeinit(&s);
s = StrInitFromZstr("-inf");
success = StrToF64(&s, &value, NULL);
result = result && (success && isinf(value) && value < 0);
StrDeinit(&s);
s = StrInitFromZstr("nan");
success = StrToF64(&s, &value, NULL);
result = result && (success && isnan(value));
StrDeinit(&s);
s = StrInitFromZstr("not a number");
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