GithubHelp home page GithubHelp logo

murilomir / decimal-calculator Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 47 KB

A module which implements functions for you to perform calculations with the decimal number system rather than the binary number system.

License: MIT License

D 100.00%

decimal-calculator's Introduction

Decimal-Calculator

This is a module written in D which implements functions for you to perform calculations with the decimal number system rather than the binary number system. The idea is to bypass the precision loss that happens as a result of the binary system, with this module you can perform FP math with a total precision of 20 decimal places or more (you can change the precision at will), even for numbers that are very big or very tiny.

To use it you only need to download the module into your project's folder and import it in your code.

It contains the functions addOrSubtract(), multiply(), divide(), power() and root(), all of which will receive as parameters 2 numbers in string format, addOrSubtract() requires a third parameter which is the operation in char format (it should be a '+' or a '-'). Remember to write the numbers properly, always write them with no unnecessary 0s and with at least 1 decimal place, don't add a + sign, do it as in "2.0" or "-1.0".

Also keep in mind it only works with powers which have an integer exponent, never try to calculate a number to the power of 1.5, or 0. And if you are calculating roots then it only works for non-negative numbers with a positive integer index, therefore never try to calculate the square root of -3.0, the 0 root of a number, the -2.0 root of a number or the 1.5 root of a number.

There is a variable called 'precision' which can be used to set the precision, it is 20 by default, but you can define it to be any number bigger than 0, as in precision = 10 or precision = 65. Never define it as 0.

E.g. addOrSubtract("2.5", "0.004", '+'), addOrSubtract("-2.5", "0.004", '-'), multiply("-2.5", "0.004"), divide("-2.5", "-0.004"), power("12.0", "4.0"), root("10.9", "4.0");

It is very fast, running on my AMD FX-4300 CPU it was able to perform 183k additions per sec, 134k subtractions per sec, 240k multiplications per sec, 31k divisions per sec, 30k powers per sec and 80 roots per sec, all with a precision of 20 digits, very impressive. I know the roots are much slower than the other operations, but it is still quite fast.

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.