GithubHelp home page GithubHelp logo

fixnum's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fixnum's Issues

Macro for const `FixedPoint` construction

We'd like to have a macro allowing to const-antly construct FixedPoints 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

Failed to deserialize number representations of FixedPoint

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)'

Trap representations

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

Better display options support

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.

Support for unsigned `Fixed`-point operations

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.

Replace macro-generated code with the the trait-based approach

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.

Optimize i128 layout

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
  • Look for a more efficient implementation of i256
  • Try to avoid i256 arithmetic when possible

Move to const generics

Integer const generic parameters are stable enough to replace the current typenum with it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.