std::ffi::c::float (v0.2.2)

Raw C bindings for floating-point text conversion.

Latest

Overview

std::ffi::c::float exposes C conversion functions such as atof, strtof, strtod, and strtold.

These return raw C results; check the end pointer or errno when conversion can fail.

Source: thrustc/std/v0.2.2/ffi/c/float.thrust

Public Signatures

Exported declarations for this module snapshot.

fn atof(text: CString)                             f64 @public @extern("atof")    @convention("C");
fn strtof(text: CString, end: ptr[ptr])            f32 @public @extern("strtof")  @convention("C");
fn strtod(text: CString, end: ptr[ptr])            f64 @public @extern("strtod")  @convention("C");
fn strtold(text: CString, end: ptr[ptr])           f80 @public @extern("strtold") @convention("C");

Behavior and Use

The conversion functions return raw C results and may set errno on failure.

Check the end pointer or errno when a conversion can fail.

Examples

import std::ffi::c::float as c;

// atof is useful at a very small C boundary, but normal code should prefer
// std::float parsing helpers.
fn readScale(text: CString) f64 @public {
    var value: f64 = c::atof(text);

    if value < 0.0 {
        return 0.0;
    }

    return value;
}
import std::ffi::c::float as c;

// strtod gives a raw C conversion with an end pointer for callers that need it.
fn parseThreshold(text: CString) f64 @public {
    var end: ptr = nullptr;
    var value: f64 = c::strtod(text, ref end);

    if end == (text as ptr) {
        return 0.0;
    }

    if value < 0.0 {
        return 0.0;
    }

    if value > 1.0 {
        return 1.0;
    }

    return value;
}
import std::ffi::c::float as c;

// Use a narrow wrapper when a C-facing configuration expects f32 values.
fn parseGain(text: CString) f32 @public {
    var end: ptr = nullptr;
    var value: f32 = c::strtof(text, ref end);

    if end == (text as ptr) {
        return 0.0 as f32;
    }

    if value < 0.0 {
        return 0.0 as f32;
    }

    return value;
}

Notes

Back to std index