ref: bdcd9c57965c02da5e30e71ff5eaeb0a71b26ca4
parent: 82eecd5a321aa3c619437a21997aa73c33c31d3c
author: S. Gilles <[email protected]>
date: Mon Sep 9 04:31:50 EDT 2019
Allow printing fltXY in scientific notation
--- a/lib/std/fltfmt.myr
+++ b/lib/std/fltfmt.myr
@@ -3,6 +3,7 @@
use "die"
use "extremum"
use "fltbits"
+use "intfmt"
use "option"
use "slpush"
use "strbuf"
@@ -16,10 +17,11 @@
pkglocal const MRelative = 2
pkglocal type fltparams = struct
- mode : int
- cutoff : size
- padto : size
- padfill : char
+ mode : int
+ cutoff : size
+ scientific : bool
+ padto : size
+ padfill : char
;;
pkglocal const flt64bfmt : (sb : strbuf#, opts : fltparams, val : flt64 -> void)
@@ -45,7 +47,7 @@
var valsb = mksb()
exp = max(exp, 1 - Dblbias)
- dragon4(valsb, false, mant, exp - 52, Dblbias, opts.mode, opts.cutoff)
+ dragon4(valsb, false, mant, exp - 52, Dblbias, opts.mode, opts.cutoff, opts.scientific)
-> blobfmt(sb, sbfin(valsb), opts, isneg)
}
@@ -66,7 +68,7 @@
var valsb = mksb()
exp = (max((exp : int64), 1 - Fltbias) : int32)
- dragon4(valsb, false, (mant : uint64), (exp - 23 : int64), Fltbias, opts.mode, opts.cutoff)
+ dragon4(valsb, false, (mant : uint64), (exp - 23 : int64), Fltbias, opts.mode, opts.cutoff, opts.scientific)
-> blobfmt(sb, sbfin(valsb), opts, isneg)
}
@@ -130,7 +132,7 @@
floating value: x = f^(e - p)
*/
-const dragon4 = {sb, isneg, f, e, p, mode, cutoff
+const dragon4 = {sb, isneg, f, e, p, mode, cutoff, scientific
var r, s, t, u, v, y
var udig
var mm, mp /* margins above and below */
@@ -137,6 +139,7 @@
var roundup
var low, high
var k
+ var tenpower : int64 = 0
var a, i
if isneg
@@ -252,6 +255,11 @@
;;
;;
+ if scientific
+ tenpower = (k - 1 : int64)
+ k = 1
+ ;;
+
if k <= 0
sbputs(sb, "0.")
for var i = 0; i < -k; i++
@@ -320,12 +328,16 @@
format(sb, 0, k--)
;;
+ if scientific
+ sbputs(sb, "e")
+ intfmt(sb, [ .base = 10 ], true, (tenpower : uint64), 64)
+ ;;
+
bigfree(u)
bigfree(r)
bigfree(s)
bigfree(mm)
bigfree(mp)
-
}
const lowdig = {u
--- a/lib/std/fmt.myr
+++ b/lib/std/fmt.myr
@@ -210,6 +210,7 @@
if startp != nbuf
param[nparam++] = (buf[startp:nbuf], "")
;;
+ startp = nbuf
| (`ParamOpt, '}'):
state = `Copy
if startp != nbuf
@@ -455,6 +456,7 @@
fp = [
.mode = MNormal,
.cutoff = 0,
+ .scientific = false,
.padfill = ' ',
.padto = 0,
]
@@ -461,6 +463,7 @@
for p : params
match p
+ | ("e", ""): fp.scientific = true
| ("w", wid): fp.padto = getint(wid, "fmt: width must be integer")
| ("p", pad): fp.padfill = decode(pad)
| ("s", sig):
--- a/lib/std/test/fltfmt.myr
+++ b/lib/std/test/fltfmt.myr
@@ -12,6 +12,8 @@
[.name = "putlowprec", .fn = putlowprec],
[.name = "padding", .fn = padding],
[.name = "sigfigs", .fn = sigfigs],
+ [.name = "scientific01", .fn = scientific01],
+ [.name = "scientific02", .fn = scientific02],
][:])
}
@@ -166,4 +168,62 @@
exp = "-10.0"
act = std.fmt("{s=-23}", f32)
testr.check(c, std.eq(exp, act), "{} should format [flt32] to \"{}\", was \"{}\"", f32, exp, act)
+}
+
+const scientific01 = {c
+ var exp, act
+ var f32 : flt32
+ var f64 : flt64
+
+ f32 = 9.125
+ exp = "9.125e0"
+ act = std.fmt("{e}", f32)
+ testr.check(c, std.eq(exp, act), "{} should format [flt32] to \"{}\", was \"{}\"", f32, exp, act)
+
+ f32 = -9.125
+ exp = "-9.125e0"
+ act = std.fmt("{e}", f32)
+ testr.check(c, std.eq(exp, act), "{} should format [flt32] to \"{}\", was \"{}\"", f32, exp, act)
+
+ f32 = 101.25
+ exp = "1.0125e2"
+ act = std.fmt("{e}", f32)
+ testr.check(c, std.eq(exp, act), "{} should format [flt32] to \"{}\", was \"{}\"", f32, exp, act)
+
+ f64 = 0.000345
+ exp = "3.45e-4"
+ act = std.fmt("{e}", f64)
+ testr.check(c, std.eq(exp, act), "{} should format [flt64] to \"{}\", was \"{}\"", f64, exp, act)
+
+ f64 = 0.0
+ exp = "0.0"
+ act = std.fmt("{e}", f64)
+ testr.check(c, std.eq(exp, act), "{} should format [flt64] to \"{}\", was \"{}\"", f64, exp, act)
+}
+
+const scientific02 = {c
+ var exp, act
+ var f32 : flt32
+ var f64 : flt64
+
+ f32 = 9.125
+ exp = "9.1e0"
+ act = std.fmt("{s=2,e}", f32)
+ testr.check(c, std.eq(exp, act), "{} should format [flt32] to \"{}\", was \"{}\"", f32, exp, act)
+
+ f32 = 9.125
+ exp = "9.1e0"
+ act = std.fmt("{e,s=2}", f32)
+ testr.check(c, std.eq(exp, act), "{} should format [flt32] to \"{}\", was \"{}\"", f32, exp, act)
+
+ f32 = 9.125
+ exp = "9.0e0"
+ act = std.fmt("{e,s=1}", f32)
+ testr.check(c, std.eq(exp, act), "{} should format [flt32] to \"{}\", was \"{}\"", f32, exp, act)
+
+ f64 = -0.000345
+ exp = "-3.4e-4"
+ act = std.fmt("{e,s=2}", f64)
+ testr.check(c, std.eq(exp, act), "{} should format [flt64] to \"{}\", was \"{}\"", f64, exp, act)
+
}