GithubHelp home page GithubHelp logo

Ncalc shows wrong result about ncalc HOT 5 CLOSED

tranleduy2000 avatar tranleduy2000 commented on May 25, 2024 2
Ncalc shows wrong result

from ncalc.

Comments (5)

jhotmann avatar jhotmann commented on May 25, 2024 2

Seems to be a floating point rounding error. It can be reproduced with something simple like:

1.3 - 1
> 0.30000000000000004

Edit: occurs with both the KaTeX and MathJax engines.
Edit2: related issue in Symja library https://bitbucket.org/axelclk/symja_android_library/issues/146/rounding-issues

from ncalc.

krzysdz avatar krzysdz commented on May 25, 2024

@poulpoulsen Could you provide us with steps to reproduce the problem (like type 0,35-0,1), your app version and LaTeX engine used?

from ncalc.

poulpoulsen avatar poulpoulsen commented on May 25, 2024

Hello there,

exactly here!
same wrong result.

Thanks
poul

from ncalc.

axkr avatar axkr commented on May 25, 2024

The reason is the double floating point arithmetic.

If you run this Java snippet it will print 0.30000000000000004

		double a = 1.3;
		double b = 1.0;
		double result = a - b;
		// prints 0.30000000000000004
		System.out.println(result);

You can use DecimalFormat to print 0.3

		DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.US);
		DecimalFormat decimalFormat = new DecimalFormat("0.0####", otherSymbols);
		// prints 0.3
		System.out.println(decimalFormat.format(result)); 

Or in Symja you can use something like this:

	public void testNumberFormat() {
		StringBuilder buf = new StringBuilder();
		try {
			DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.US);
			DecimalFormat decimalFormat = new DecimalFormat("0.0####", otherSymbols);
			OutputFormFactory factory = OutputFormFactory.get(true, false, decimalFormat);

			IExpr expr = F.num("12345.123456789");
			factory.convert(buf, expr);
		} catch (IOException e) {
			e.printStackTrace();
		}
		assertEquals(buf.toString(), "12345.12346");
	}

from ncalc.

visionok avatar visionok commented on May 25, 2024

I was using NCalc+ to check my groceries bill when I came across this error. This is seriously broken for a calculator! The real solution is to use something like BCDs. Using something like DecimalFormat suggested above only hides the errors.

from ncalc.

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.