Git Product home page Git Product logo

Comments (12)

Qix- avatar Qix- commented on July 22, 2024 1

Try this:

/\^\s*((?:\d{5,}(?:\.\d*)?)|(?:\d{5,}?(?:\.\d*)))/g

Test it here.

from algebrite.

davidedc avatar davidedc commented on July 22, 2024

Sounds good. I wonder whether this approximation should only happen with float() (or when a float appears in the expression, which triggers numeric evaluation too). Once that happens though, since floats are "toxic", all results would be numeric from then on, we'd need a way to promote a float into a non-float in case one wants to use the result in non-numeric ways. There is a routine I'm working on to guess a symbolic form for a float, could use that.

from algebrite.

murkle avatar murkle commented on July 22, 2024

@davidedc do you know of the PSLQ Algorithm? That is very powerful way to "guess" the float -> exact conversion eg (sqrt(2)+1)/2, pi^2+1
http://mathworld.wolfram.com/PSLQAlgorithm.html

from algebrite.

davidedc avatar davidedc commented on July 22, 2024

yes eventually that's the ambition. For step 1, there is a surprising lot that can be guessed with quick and un-elegant trial and error routines. If anyone wants to take on PSLQ though...

from algebrite.

davidedc avatar davidedc commented on July 22, 2024

in fact approxratio(7.9488357 * 10^328927) already works (gives 24236/3049*10^328927). Works in zeno branch, I don't think it works in master yet. (In fact I think Michael @murkle gave me pointers to approxratio routine).

from algebrite.

bloc97 avatar bloc97 commented on July 22, 2024

This does not need to be a float, since 10^(fractional part) eg. 10^(0.8542786512636754831467...) can be done very fast, even as a bignumber. Does Algebrite currently support choosing the precision for logs and exponentiation?

from algebrite.

bloc97 avatar bloc97 commented on July 22, 2024

Something like this works for me right now...

const Algebrite = require("Algebrite");

const exp = "90000000^8000000";
let result = "";

if (exp.search(/\^\d\d\d\d\d/) > -1) { //if the exponent is bigger than 9999.
                                                           //matches any string that contains ^nnnnn
	console.log("Infinite precision arithmethic would take too long, falling back to approximation.")
	
	const base = exp.slice(0, exp.indexOf("^"));
	const exponent = exp.slice(exp.indexOf("^")+1, exp.length);
	
	const exponentb10 = Math.log10(base) * exponent;
	
	const significand = Math.pow(10, exponentb10%1); //This part can allow choosing precision by using bignumber
	const base10 = Math.floor(exponentb10);
	
	result = significand + "e" + base10;
	
} else {
	result = Algebrite.run(exp).toString();
}
console.log(result); //Outputs 1.1899113297459072e63633940

from algebrite.

davidedc avatar davidedc commented on July 22, 2024

@bloc97 nice, I'll use this under MIT and credit if it's OK.

Yes sorry by float I mean: Algebrite goes into "numeric" mode (using JS Numbers) only when the expression contains a float() call or contains a number with a decimal point plus a few other cases such as when using nroots. Otherwise, it returns symbolic results (which are expression trees containing rationals as long-integer num/denom pairs, constants such as PI and E and all the operations such as SQRT etc.).

So I'm thinking this approximation should probably only kick-in when doing something like float(90000000^8000000) or 90000000.0^8000000.0 or analogous, and left as-is (unevalled) otherwise.

No there is no arbitrary precision in numerical calculations (yet), floats are just JS Numbers at the moment (i.e. the 64 bits floats sometimes called doubles).

from algebrite.

bloc97 avatar bloc97 commented on July 22, 2024

Sure, but the regex was hacked in and not really robust, if someone typed in 500 ^ 60000 it wouldn't work, and it would also break if someone did 500^500^500000, so it would be best to parse the input and simplify it as one single operation before doing regex.
Perhaps you already have a solution to this within Algebrite itself?

from algebrite.

davidedc avatar davidedc commented on July 22, 2024

ah yes sure. I think that a check when evalling the exponential operator should be enough, I'll try something.

from algebrite.

davidedc avatar davidedc commented on July 22, 2024

just to give a heads up: while the regex solution works great for the routine above that lives "outside" of the Algebrite core, the solution from "within" the Algebrite core will just check the size of the two numbers with the available bigint comparison operators (or the existing rational number comparison operators)

from algebrite.

Qix- avatar Qix- commented on July 22, 2024

Even better :)

from algebrite.

Related Issues (20)

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.