Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
/// /// A simple and very fast float parser in Zig. /// /// Written by Tetralux@teknik.io, 2019-09-06. /// /// /// Will error if you have too many decimal places, /// invalid characters within the number, or an /// empty string. /// /// Is also subject to rounding errors. /// const std = @import("std"); const mem = std.mem; const time = std.time; const math = std.math; const inf = math.inf; const nan = math.nan; const warn = std.debug.warn; inline fn toDigit(ch: u8) !u8 { if (ch >= '0' and ch <= '9') return ch - '0'; return error.InvalidCharacter; } fn parseFloat(comptime T: type, slice: []const u8) error{Empty, InvalidCharacter, TooManyDigits}!T { var s = mem.separate(slice, " ").next() orelse return error.Empty; if (s.len == 0) return error.Empty; var is_neg = s[0] == '-'; if (is_neg) { if (s.len == 1) return error.Empty; s = s[1..]; } if (mem.eql(u8, s[0..3], "inf")) return if(is_neg) -inf(T) else inf(T); if (mem.eql(u8, s[0..3], "nan")) return nan(T); // -nan makes no sense. // Read the digits into an integer and note // where the decimal point is. var n: u64 = 0; var decimal_point_index: isize = -1; var decimal_places: usize = 0; var numeral_places: usize = 0; for (s) |ch, i| { if (ch == '.') { decimal_point_index = @intCast(isize, i); continue; } if (decimal_point_index == -1) numeral_places += 1 else decimal_places += 1; n += try toDigit(ch); n *= 10; } if (decimal_places + numeral_places > 18) return error.TooManyDigits; // f64 has 18 s.f. var n_as_float = @intToFloat(f64, n) / 10; // Shift the decimal point into the right place. if (decimal_point_index != -1) { // We counted from the front, we'll insert the decimal point from the back. const decimal_point_index_from_back = @intCast(isize, s.len) - decimal_point_index - 1; { var i: isize = 0; while (i < decimal_point_index_from_back) : (i += 1) { n_as_float /= 10; } } } var res = @floatCast(T, n_as_float); if (is_neg) res *= -1; return res; } pub fn main() !void { { var total: u64 = 0; const its = 10240000; var i: u64 = 0; while (i < its) : (i += 1) { var t = try time.Timer.start(); var f = try parseFloat(f64, "4.77777777777777777"); var took = t.read(); // warn("f is {d}\n", f); // break; total += took; } warn("average time: {d} ns\n", @intToFloat(f64, total) / @intToFloat(f64, its)); } { var total: u64 = 0; const its = 10240000; var i: u64 = 0; while (i < its) : (i += 1) { var t = try time.Timer.start(); var f = try std.fmt.parseFloat(f64, "4.77777777777777778"); var took = t.read(); total += took; } warn("average time: {d} ns\n", @intToFloat(f64, total) / @intToFloat(f64, its)); } }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree