Giter Site home page Giter Site logo

fdiv's Introduction

Fdiv - Fixed Point Arithmetic in Shell Scripts GitHub last commit

Introduction

fdiv bash script takes two integer arguments: numerator and denominator.

It returns a fixed point number division result to the best possible precision allowed by signed 64 bit integers, rounded to the nearest next digit.

Saves having to use bulky external facilities in subshells, such as awk,dc,bc, for very simple calculations.

fadd takes two positive fixed point numbers, such as produced by the above, and adds them up.

Other operators are still to come.

Installation

Simply place the fdiv and fadd scripts in any directory in your search path, such as $HOME/bin, /usr/local/bin, or specify its path when invoking it. Make sure that they have execute permissions.

Example Usage

fdiv -2 3
-0.666666666666666667  
fdiv 355 113
3.14159292035398230
fadd 9.876543 0.987
10.863543

Features

  • Checks arguments, returns errors if they are missing or not integers or the divisor (denominator) is zero. Deals correctly with negative argument(s).

  • When the numerator is zero, 0.0 is immediately returned.

  • Automatically calculates with the maximum precision. Truncation error will be less than 5E-19.

  • When numerator > denominator, then the answer is composed from numerator/denominator integer division, followed by the decimal point and then the upscaled remainder divided by the denominator, which gives the digits after the decimal point. This maintains the 5E-19 guaranteed precision even for some quite large numbers. For example:

fdiv $((2**63-1)) 3
3074457345618258602.333333333333333333
  • When the denominator is divisible by 10, or its multiples, then the decimal precision can be also increased, this time by the relevant number of zeroes inserted after the decimal point. For example, here the truncation error is now less than 5E-22:
fdiv 5 30000
0.0001666666666666666667

Note that this is not like Bignum package using an unlimited number of words and storage size, so barring the mod 10 trick, there are natural limits to the precision obtained: namely two 64 bit words. Still, that is quite a lot better than the 52 bits of the 'double precision' floating point standard.

References

See comments in the source of fdiv for more details.

Here is a blog describing the magic idea behind implementing this using only Bash integer arithmetic.

fdiv's People

Contributors

liborty avatar

Watchers

 avatar  avatar

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.