Library "fraction" Fraction Creation and Basic Operations. Cracked a tough problem in making this Polarity Agnostic Decimal without a cheating "abs * sign of input".
it's quite fast, however still test for errors before production use. >> Big Neon Sign on 1/0 value. <<
Int Array (LOC 0/1)..
To/From Decimal(float)
Comparison ( < / == / >)
Add / Sub / Mult / Div
Invert polarity +/-
String output with 2 formats ..
make(_numerator, _denominator, _val) Parameters: _numerator: (int) above the line integer ie: ____ of (___ / bottom ) _denominator: (int) below the line integer ie: ____ of (top / ______ ) _val: (int) OPTIONAL (for no real reason including it) integer to multiply Returns: array<int> where index 0 is Numerator, 1 is Denominator
add(_fraction, _fraction2) Perform add operation (left adds right onto ) Parameters: _fraction: (array<int>) left side Fraction Object _fraction2: (array<int>) right side Fraction Object Returns: array<int> where index 0 is Numerator, 1 is Denominator
subtract(_fraction, _fraction2) Perform subtract operation (left subtracts right from ) Parameters: _fraction: (array<int>) left side Fraction Object _fraction2: (array<int>) right side Fraction Object Returns: array<int> where index 0 is Numerator, 1 is Denominator
multiply(_fraction, _fraction2) Perform multiply operation (left multiplies by right ) Parameters: _fraction: (array<int>) left side Fraction Object _fraction2: (array<int>) right side Fraction Object Returns: array<int> where index 0 is Numerator, 1 is Denominator
divide(_fraction, _fraction2) Perform divide operation (left divides by right ) Parameters: _fraction: (array<int>) left side Fraction Object _fraction2: (array<int>) right side Fraction Object Returns: array<int> where index 0 is Numerator, 1 is Denominator
negative(_fraction) Perform Negative number inversion ie: (-1/2 => 1/2) or (3/5 => -3/5) Parameters: _fraction: (array<int>) Fraction Object to invert to/from negative Returns: array<int> where index 0 is Numerator, 1 is Denominator
isSmaller(_fraction, _fraction2) Check if first fraction is smaller Parameters: _fraction: (array<int>) left side Fraction Object _fraction2: (array<int>) right side Fraction Object Returns: True if smaller, false if bigger
isLarger(_fraction, _fraction2) Check if first fraction is larger Parameters: _fraction: (array<int>) left side Fraction Object _fraction2: (array<int>) right side Fraction Object Returns: True if smaller, false if bigger
isEqual(_fraction, _fraction2) Check if first fraction is equal Parameters: _fraction: (array<int>) left side Fraction Object _fraction2: (array<int>) right side Fraction Object Returns: True if smaller, false if bigger
fromDec(_input, _epsilon, _iterations) Convert Decimal to Fraction array note : this is my own Negative Number Capable (tiny speed loss) adaptation of the fastest algo out there Exclusive for Tradingview. Parameters: _input: (float) Decimal Input _epsilon: (int) (OPTIONAL) to precision 0's after dec 0.0000 -> epsilon 0's _iterations: (int) (OPTIONAL) Maximum iterations Till give up Returns: array<int> where index 0 is Numerator, 1 is Denominator
toDec() Convert Fraction to Decimal Output Returns: Float of fration
toString(_fraction) Create "A/B" or "A and B/C" String Value of Fraction. Parameters: _fraction: (array<int>) Fraction Object to invert to/from negative Returns: String as (-)? A and B/C format
In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in a publication is governed by House rules.
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.