Comments (18)
Perhaps just extend float_fmt with a mode and a precision arg?
from motoko-base.
Using specifier "%.16g" not "%f" yields better result, but I don't know if that's good enough.
rts/float.c
export as_ptr float_fmt(double a) {
extern int snprintf(char *__restrict, size_t, const char *__restrict, ...);
char buf[50]; // corresponds to 150 bits of pure floatness, room for 64 bits needed
// const int chars = snprintf(buf, sizeof buf, "%f", a);
const int chars = snprintf(buf, sizeof buf, "%.17g", a);
return text_of_ptr_size(buf, chars);
}
from motoko-base.
@rossberg any advice here?
from motoko-base.
Yes, %.17g
is enough to roundtrip, I've used that in the Wasm interpreter, and it works for all the many corner cases in the test suite that exercise bit-precise rounding behaviour (at least OCaml's implementation of the formatting, which I assume is inherited from C).
However, in practice, users may not want to see umteen zeroes all the time. We'll have to add some richer formatting functions; toText should probably default to %f
.
There's also %h
, of course.
from motoko-base.
Something like toText(f : Float, prec : ?Nat8)
would be easily realisable (in the compiler, for the interpreter probably hairier).
from motoko-base.
@ggreif, I think it should be a separate function from toText, though. But then we'd probably want s.th like SML's realfmt type, maybe extended with a hex
case for %h
.
In the interpreter that ought to be trivial (using OCaml's Printf).
from motoko-base.
@rossberg In OCaml we can't build the format string dynamically, can we?
val fmt : string = "%.9g"
# Printf.sprintf fmt 3.14159;;
Error: This expression has type string but an expression was expected of type
('a -> 'b, unit, string) format =
('a -> 'b, unit, string, string, string, string)
CamlinternalFormatBasics.format6
from motoko-base.
Following SML's basis library, here is what I propose: a function Float.toFormattedText : (Float, Format) -> Text
, where the type Float.Format
is defined as follows:
type Format = {
#fix : Nat; // like "%.*f"
#exp : Nat; // like "%.*e"
#gen : Nat; // like "%.*g"
#hex : Nat; // like "%.*h" a.k.a. "%.*a"
#exact; // like "%.17g"
}
(Making precision non-optional, since that doesn't buy you much in this form of description.)
from motoko-base.
@ggreif, no, but a simple switch over the 4 or 5 formats is enough. The precision can be turned into a dynamic argument by using *
.
from motoko-base.
That's what I mean by "hairier".
from motoko-base.
How is that "hairy"?
from motoko-base.
It has more hair :-)
from motoko-base.
But we'll still need some supporting primitives for this to fly, right? (I'll come up with a PR in dfinity/motoko soon.)
from motoko-base.
Great, thanks guys. I'll leave that in your hands then @ggreif.
from motoko-base.
I totally misunderstood what "%.*f"
means. I thought that the *
is purely meta-notation. Another thing learned...
from motoko-base.
Now I see why you considered it hairy. :)
from motoko-base.
Now I see why you considered it hairy. :)
Precisely.
from motoko-base.
Is there any way we can not have toFormattedText
at all and make it very very clear in the documentation that a simple toText
is not for displaying to users? Formatting floating point numbers for human consumption is a locale-dependent function, as there are many different ways that different cultures format numbers with varying thousands separators, digit groupings, decimal separators, rounding rules, etc. A separate number formatter class that takes a locale would be ideal and would keep floats and natural numbers clean and small.
from motoko-base.
Related Issues (20)
- Deque.size() is missing
- Stack overflow for Heap.fromIter
- AssocList is missing keys(), vals() HOT 1
- bug: undocumented functions
- Class `SHA224` in `Principal.mo` contains dead data HOT 1
- Array.chain(): Error "index out of bounds"
- Principal.mo uses deprecated function HOT 5
- sequence-like collections should have a `group` operation that lumps together equal subsequences
- utilities for tuple comparisons
- FR: consider adding List.contains : (List<A>, A, (A,A) -> Bool) -> Bool (or similar)
- FR: consider adding module `VarArray` with mutable versions of the immutable array functions in `Array`
- chore: test `viper` again
- Motoko Base gives warnings on DFX deploy. HOT 2
- Principal - new functions | toSubaccount | toICRC1Text
- Would Dfinity like to take over publishing base package on Mops? HOT 5
- chore: remove unused identifiers in base and tests HOT 1
- rename `toLedgerAccount` to `toIcpLedgerAccountIdentifier` to avoid confusion
- Recurring Timer Cancelling HOT 4
- Blob.size is missing HOT 1
- Clarify Float.toInt is truncating
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from motoko-base.