kaigouthro

fraction

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
Thư viện Pine

Với tinh thần TradingView thực sự, tác giả đã xuất bản mã Pine này như một thư viện mã nguồn mở để các lập trình viên Pine khác từ cộng đồng của chúng tôi có thể sử dụng lại nó. Chúc mừng tác giả! Bạn có thể sử dụng thư viện này một cách riêng tư hoặc trong các ấn phẩm mã nguồn mở khác, nhưng việc sử dụng lại mã này trong một ấn phẩm chịu sự điều chỉnh của Nội quy chung.

Thông báo miễn trừ trách nhiệm

Thông tin và ấn phẩm không có nghĩa là và không cấu thành, tài chính, đầu tư, kinh doanh, hoặc các loại lời khuyên hoặc khuyến nghị khác được cung cấp hoặc xác nhận bởi TradingView. Đọc thêm trong Điều khoản sử dụng.

Bạn muốn sử dụng thư viện này?

Sao chép văn bản vào khay nhớ tạm và dán nó vào tập lệnh của bạn.