loyd / fixnum Goto Github PK
View Code? Open in Web Editor NEWFixed-point numbers
License: MIT License
Fixed-point numbers
License: MIT License
Hi!
Parity Scale Codec version 3 is out, but we iroha#2158 can't upgrade, because FixNum
doesn't support the latest version.
Would you accept a PR that would bump the version to 3?
We'd like to have a macro allowing to const
-antly construct FixedPoint
s out of literals:
fixed_point!(3) //=> 3.0
fixed_point!(0.12) //=> 0.12
fixed_point!(-123_456.789_987) //=> -123456.789987
Extra:
fixed_point!(3%) //=> 0.03
fixed_point!(3.45e-4) //=> 0.000345
Working with 3rd party API which represents floats as numbers in JSON I failed to use FixedPoint directly in my structures. During deserialization it fails.
Test Case (integer):
use fixnum::typenum::U18;
use serde::Deserialize;
pub type FixedPoint = fixnum::FixedPoint<i128, U18>;
#[test]
fn test_fixnum() {
#[derive(Deserialize)]
struct Test {
fix_point: FixedPoint,
}
serde_json::from_str::<Test>(r#"{"fix_point": 270)"#).unwrap();
}
Expected Result:
No panic.
Actual Result:
thread 'test_fixnum' panicked at 'called `Result::unwrap()` on an `Err` value: Error("invalid type: integer `270`, expected a FixedPoint type representing a fixed-point number", line: 1, column: 17)'
Test Case (float):
use fixnum::typenum::U18;
use serde::Deserialize;
pub type FixedPoint = fixnum::FixedPoint<i128, U18>;
#[test]
fn test_fixnum() {
#[derive(Deserialize)]
struct Test {
fix_point: FixedPoint,
}
serde_json::from_str::<Test>(r#"{"fix_point": 270.0)"#).unwrap();
}
Expected Result:
No panic.
Actual Result:
thread 'test_fixnum' panicked at 'called `Result::unwrap()` on an `Err` value: Error("invalid type: floating point `270`, expected a FixedPoint type representing a fixed-point number", line: 1, column: 19)'
Hi,
am I safe to assume that there are no trap representations of FixedPoint<I, U>
? To paraphrase, can FixedPoint
always be safely transmuted to and from I
? I have the feeling this is so, but I'd like a confirmation
It's wonderful that we're able to take fractional numeric formatting options for convenient formatting syntax.
let x: FloatingPoint<i128, U18>;
format!("{0:.5}", x)
Let's support that stuff!
Probably it willl be useful to pass through single named formatting option to provide serialization strategy.
Line 22 in 90c0411
Sometimes it is obvious that a number cannot be negative (or zero). It is also true of fixed point numbers.
Given this, it would be nice if Fixed
supported using the extra bit for the precision, e.g.
Fixed<u64>
which itself starts from zero, and doesn't carry the sign bit. Internally promotes to u128
just like i64
.Fixed<NonZeroU64>
. Same as before except it also has a niche.The implementations for cadd
, csub
etc. need to be adjusted accordingly.
Now, we generate extra code for every FixedPoint<I, P>
instead of using the trait system. The trait-based approach allows removing extra features (i16
, i32
, and so on), produces better documentation, and allows defining custom layouts.
It is also related to #33.
FixedPoint based on i128 promotes to i256 during calculations. It significantly reduces performance, e.g.:
F64p9/rmul (~1e4, Nearest) 1.00 1.9±0.00ns
F128p18/rmul (~1e4, Nearest) 1.00 134.1±0.79ns
Now, the FromStr
implementation doesn't accept the scientific notation. We need to fix it.
Integer const generic parameters are stable enough to replace the current typenum
with it.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.