Git Product home page Git Product logo

Comments (6)

trusktr avatar trusktr commented on July 17, 2024

Wait, I see a a few examples if I scroll down.

So if I understand properly, the following would sum 5 and 8 together: fn["+"](5, 8).

So to do (5+8)*(3-6) it would be:

fn["*"]( fn["+"](5, 8), fn["-"](3, 6) )

And if I want to do 10%3, that would be: fn.mod(10, 3) which would result in 1.
(I noticed there is no fn["%"](a, b) version of mod, which would be nice.)

Do I have the syntax right?

from javascript-bignum.

trusktr avatar trusktr commented on July 17, 2024

One more question, so to use big numbers, do I need to wrap them in quotes, or just use them literally?

e.g., fn["*"](8833943883332344019357498834, 109873475987293847960119823745795555.234) Does that work?

I'll get to try it soon.

from javascript-bignum.

trusktr avatar trusktr commented on July 17, 2024

By the way, putting some nice usage examples here on the github page might attract more users.

from javascript-bignum.

jtobey avatar jtobey commented on July 17, 2024

@trusktr

Thanks for the suggestions. For more complete examples, please see the examples directory, specifically this exact arithmetic example and this complex numbers example. Both examples build a small abstraction layer around javascript-bignum to facilitate switching to native arithmetic or another library. The Moebius example also replaces the clunky function names like fn["+"] with names inspired by C like "cadd".

You have to quote the numbers, or else you lose exactness and end up with normal JavaScript arithmetic. For example, alert(8833943883332344019357498834) gives "8.833943883332344e+27". The extra precision is lost as soon as soon as the browser parses the number, before the library can see it. Scheme can mix exact and inexact numbers, but the result is usually inexact. We have fn["exact?"] to know for sure.

from javascript-bignum.

trusktr avatar trusktr commented on July 17, 2024

Cool. Thanks for that great info and links.

By "inexact", do you mean for example that 3.141592653589793238462643383279502884197169399375105820974944592307816406286 (pi) would be considered inexact? So if you add 1/3 and 1/3 you'd get 2/3, but if you convert it to decimal form at any level of precision, it'd be inexact? Is that what you mean when you refer to "inexact"?

from javascript-bignum.

jtobey avatar jtobey commented on July 17, 2024

It's a little more than that. In Scheme (and javascript-bignum) every number value is tagged either exact or inexact. "3.14159" would be inexact due to the decimal, but "#e3.14159" would be exact, because of the exactness prefix "#e". However, it would not be exactly pi, it would be 314159/100000.

Similarly, "1/3" is exact, but "#i1/3" is inexact. You can even have inexact integers, such as "#i42" or its equivalent "42.0". fn["/"]("1","3") would give you exact 1/3, since both operands are exact, but fn["/"]("1.","3") would give an inexact since "1." is inexact (equivalent to "1.0"). In javascript-bignum (and typically in Scheme) inexact reals are just IEEE doubles, the same as JavaScript numbers.

By "convert it to decimal" I take you to mean conversion to a string using the characters '0' to '9' and possibly '-' '.' or exponential notation. Strings are not numbers, so the exactness property does not apply. If you mean, would the string yield an exact number if passed to the library, that depends on how you converted it. fn["number->string"] preserves exactness, appending "." or using "a/b" form if necessary. The ECMAScript methods (toFixed, etc.) do not.

Please consult the Scheme standard for full details.

from javascript-bignum.

Related Issues (10)

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.