Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue with large numbers #16

Open
gabrielalexandrelopes opened this issue Nov 8, 2024 · 2 comments
Open

issue with large numbers #16

gabrielalexandrelopes opened this issue Nov 8, 2024 · 2 comments
Labels
bug Something isn't working
Milestone

Comments

@gabrielalexandrelopes
Copy link

in typst floats are 64 bits, meaning we should be able to handle large numbers such as 10e300. Unfortunately oxifmt is doing a computation that goes out of bounds that prevents writing the typst code:

#strfmt("{0:e}",10000000000000000000)

In my case I'm reading numbers from a json file in typst and using strfmt to display them. Can you fix this?

error: the result is too large
    ┌─ @preview/oxifmt:0.2.1/oxifmt.typ:228:21
    │
228 │   let mantissa = f / calc.pow(10, exponent)
    │                      ^^^^^^^^^^^^^^^^^^^^^^

help: error occurred in this call of function `_strfmt_exp-format`
    ┌─ @preview/oxifmt:0.2.1/oxifmt.typ:374:20
    │
374 │       replacement = _strfmt_exp-format(calc.abs(replacement), exponent-sign: exponent-sign, precision: precision)
    │                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@PgBiel
Copy link
Owner

PgBiel commented Nov 8, 2024

Ah, good catch! Will be taking a look once I can.

@PgBiel PgBiel added this to the 1.0.0 milestone Nov 8, 2024
@PgBiel PgBiel added the bug Something isn't working label Nov 8, 2024
@gabrielalexandrelopes
Copy link
Author

by the way, I got a temporary solution:

#let toMathRepresentation(number, digits: 2) = {

  if (type(number) != "float" and type(number) != "int") {
    [N/A]
  } else {
    // Define the maximum exponent to prevent overflow
    let max_exponent = 308;

    // Calculate the exponent
    let exponent = calc.floor(calc.log(calc.abs(number)));

    if exponent > max_exponent {
        "Infinity"
    } else {
        // Initialize mantissa
        let scaled_number = calc.abs(number);
        let remaining_exponent = exponent;

        // Scale down by powers of 10 within Typst's limits
        while remaining_exponent > 0 {
            // Determine the largest power of 10 we can divide by without overflow
            let scale_down = calc.min(remaining_exponent, 18);
            scaled_number = scaled_number / calc.pow(10, scale_down);
            remaining_exponent -= scale_down;
        }

        // Once scaled, the result is within [1, 10), so this is our mantissa
        let mantissa = scaled_number;

        // Round the mantissa to the specified number of digits
        let rounded_mantissa = calc.round(mantissa*calc.pow(10,digits))/calc.pow(10,digits);

        // Format the result
        [$#rounded_mantissa times 10^(#exponent)$]
    }
    }
};

maybe you can use this. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants