Coder Social home page Coder Social logo

Comments (8)

stencillogic avatar stencillogic commented on May 26, 2024

round is the function that can round fractional part of a number.
What's the value of this ROUNDING_MODE ?

from astro-float.

stencillogic avatar stencillogic commented on May 26, 2024

Also, rounding will not change exponent of a number, unless there was overflow during rounding.

from astro-float.

Janmajayamall avatar Janmajayamall commented on May 26, 2024

What's the value of this ROUNDING_MODE ?

It is set to none - RoundingMode::None

from astro-float.

Janmajayamall avatar Janmajayamall commented on May 26, 2024

Ok, so seems like the only way is to read the mantissa after rounding as a string. Drop off digits after decimal as per the exponent. Then parse the resulting string of digits into BigUint. Do you think there's a better way to do this?

from astro-float.

Janmajayamall avatar Janmajayamall commented on May 26, 2024

This is what I came up with:

let mut consts = Consts::new().unwrap();

let mut value = AstroBFloat::from(1.2);

let (sign, radix_repr, mut ex) = value
    .round(0, ROUNDING_MODE)
    .convert_to_radix(astro_float::Radix::Dec, ROUNDING_MODE, &mut consts)
    .unwrap();

if ex < 0 {
    ex = 0;
}

// Assume sign is always positive
let value_biguint = BigUint::from_radix_be(&radix_repr[..ex as usize], 10).unwrap();

from astro-float.

stencillogic avatar stencillogic commented on May 26, 2024

round() with RoundingMode::None has no effect: docs.

The most efficient way in my opinion is to get raw parts of big float, and then construct big integer from the mantissa slice after accomplishing required bit shift.

from astro-float.

Janmajayamall avatar Janmajayamall commented on May 26, 2024

Sorry for late reply!

Is this what you had in mind ?

// PRECISION = 256
// MANTISSA_LEN = 4
let value = value.0.clone();
let (raw, _, s, _, _) = value.as_raw_parts().unwrap();
let exponent = value.exponent().unwrap();

let mut bits = exponent;
let mut index = MATISSA_LEN - 1;
let mut res = BigInt::zero();
while bits > 0 {
    res += (BigInt::from_u64(raw[index as usize]).unwrap())
        << (astro_float::WORD_BIT_SIZE * index);
    bits -= astro_float::WORD_BIT_SIZE as i32;
    index -= 1;
}
res >>= (PRECISION as i32) - exponent;
res = if s.is_negative() { res.neg() } else { res }

from astro-float.

stencillogic avatar stencillogic commented on May 26, 2024

Closing the issue as it is not a bug.

from astro-float.

Related Issues (19)

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.