waves_logo Docs
  • Ride Programming Language
    Ride Programming Language
  • Getting Started
    Getting Started
  • Syntax Basics
    • Directives
      Directives
    • Definition
      Definition
    • Expression
      Expression
    • Constant
      Constant
    • Variable
      Variable
    • Function
      Function
    • Exception
      Exception
    • Comment
      Comment
    Syntax Basics
  • Script Types
    • dApp Script
      • Annotations
        Annotations
      • Callable Function
        Callable Function
      • Verifier Function
        Verifier Function
      dApp Script
    • Account Script
      Account Script
    • Asset Script
      Asset Script
    Script Types
  • Standard Library
    • Version 5: What’s New
      Version 5: What’s New
    Standard Library
  • Data types
    • Any
      Any
    • BigInt
      BigInt
    • Boolean
      Boolean
    • ByteVector
      ByteVector
    • Int
      Int
    • String
      String
    • Unit
      Unit
    • List
      List
    • Tuple
      Tuple
    • Union
      Union
    Data types
  • Structures
    • Script Actions
      • BinaryEntry
        BinaryEntry
      • BooleanEntry
        BooleanEntry
      • Burn
        Burn
      • DeleteEntry
        DeleteEntry
      • IntegerEntry
        IntegerEntry
      • Issue
        Issue
      • Lease
        Lease
      • LeaseCancel
        LeaseCancel
      • Reissue
        Reissue
      • ScriptTransfer
        ScriptTransfer
      • SponsorFee
        SponsorFee
      • StringEntry
        StringEntry
      Script Actions
    • Common Structures
      • Address
        Address
      • Alias
        Alias
      • Asset
        Asset
      • AssetPair
        AssetPair
      • AttachedPayment
        AttachedPayment
      • BalanceDetails
        BalanceDetails
      • BlockInfo
        BlockInfo
      • Invocation
        Invocation
      • Order
        Order
      • Transfer
        Transfer
      Common Structures
    • Transaction Structures
      • BurnTransaction
        BurnTransaction
      • CreateAliasTransaction
        CreateAliasTransaction
      • DataTransaction
        DataTransaction
      • ExchangeTransaction
        ExchangeTransaction
      • GenesisTransaction
        GenesisTransaction
      • InvokeScriptTransaction
        InvokeScriptTransaction
      • IssueTransaction
        IssueTransaction
      • LeaseCancelTransaction
        LeaseCancelTransaction
      • LeaseTransaction
        LeaseTransaction
      • MassTransferTransaction
        MassTransferTransaction
      • ReissueTransaction
        ReissueTransaction
      • SetAssetScriptTransaction
        SetAssetScriptTransaction
      • SetScriptTransaction
        SetScriptTransaction
      • SponsorFeeTransaction
        SponsorFeeTransaction
      • TransferTransaction
        TransferTransaction
      • UpdateAssetInfoTransaction
        UpdateAssetInfoTransaction
      Transaction Structures
    Structures
  • Built-in Variables
    Built-in Variables
  • Built-in Functions
    • Account Data Storage Functions
      Account Data Storage Functions
    • Blockchain Functions
      Blockchain Functions
    • Byte Array Functions
      Byte Array Functions
    • Converting Functions
      Converting Functions
    • dApp-to-dApp Invocation Function
      dApp-to-dApp Invocation Function
    • Data Transaction Functions
      Data Transaction Functions
    • Decoding Functions
      Decoding Functions
    • Encoding Functions
      Encoding Functions
    • Exception Functions
      Exception Functions
    • Hashing Functions
      Hashing Functions
    • List Functions
      List Functions
    • Math Functions
      Math Functions
    • String Functions
      String Functions
    • Union Functions
      Union Functions
    • Verification Functions
      Verification Functions
    Built-in Functions
  • Operators
    Operators
  • Pattern Matching: match-case
    Pattern Matching: match-case
  • Iterations with FOLD<N>
    Iterations with FOLD<N>
  • dApp-to-dApp Invocation
    dApp-to-dApp Invocation
  • Limitations
    • Complexity
      Complexity
    • Data Weight
      Data Weight
    Limitations
  • Version 6 (Testnet, Stagenet)
    • Callable Function
      Callable Function
    • Structures
      • Invocation
        Invocation
      Structures
    • Built-in Functions
      • Byte Array Functions
        Byte Array Functions
      • Converting Functions
        Converting Functions
      • Hashing Functions
        Hashing Functions
      • Math Functions
        Math Functions
      • String Functions
        String Functions
      • Verification Functions
        Verification Functions
      Built-in Functions
    • Limitations
      Limitations
    Version 6 (Testnet, Stagenet)
  • Previous Versions: 4 and 3
    • Callable Function
      Callable Function
    • Data types
      • Boolean
        Boolean
      • ByteVector
        ByteVector
      • Int
        Int
      • String
        String
      • Unit
        Unit
      • List
        List
      • Tuple
        Tuple
      • Union
        Union
      Data types
    • Structures
      • Script Actions
        • BinaryEntry (v4)
          BinaryEntry (v4)
        • BooleanEntry (v4)
          BooleanEntry (v4)
        • Burn (v4)
          Burn (v4)
        • DataEntry (v3)
          DataEntry (v3)
        • DeleteEntry (v4)
          DeleteEntry (v4)
        • IntegerEntry (v4)
          IntegerEntry (v4)
        • Issue (v4)
          Issue (v4)
        • Reissue (v4)
          Reissue (v4)
        • ScriptTransfer (v3 and v4)
          ScriptTransfer (v3 and v4)
        • SponsorFee (v4)
          SponsorFee (v4)
        • StringEntry (v4)
          StringEntry (v4)
        Script Actions
      • Script Results (v3)
        • ScriptResult
          ScriptResult
        • TransferSet
          TransferSet
        • WriteSet
          WriteSet
        Script Results (v3)
      • Common Structures
        • Address
          Address
        • Alias
          Alias
        • Asset
          Asset
        • AssetPair
          AssetPair
        • AttachedPayment
          AttachedPayment
        • BalanceDetails
          BalanceDetails
        • BlockInfo
          BlockInfo
        • Invocation
          Invocation
        • Order
          Order
        • Transfer
          Transfer
        Common Structures
      • Transaction Structures
        • BurnTransaction
          BurnTransaction
        • CreateAliasTransaction
          CreateAliasTransaction
        • DataTransaction
          DataTransaction
        • ExchangeTransaction
          ExchangeTransaction
        • GenesisTransaction
          GenesisTransaction
        • InvokeScriptTransaction
          InvokeScriptTransaction
        • IssueTransaction
          IssueTransaction
        • LeaseCancelTransaction
          LeaseCancelTransaction
        • LeaseTransaction
          LeaseTransaction
        • MassTransferTransaction
          MassTransferTransaction
        • ReissueTransaction
          ReissueTransaction
        • SetAssetScriptTransaction
          SetAssetScriptTransaction
        • SetScriptTransaction
          SetScriptTransaction
        • SponsorFeeTransaction
          SponsorFeeTransaction
        • TransferTransaction
          TransferTransaction
        • UpdateAssetInfoTransaction
          UpdateAssetInfoTransaction
        Transaction Structures
      Structures
    • Built-in Variables
      Built-in Variables
    • Built-in Functions
      • Account Data Storage Functions
        Account Data Storage Functions
      • Blockchain Functions
        Blockchain Functions
      • Byte Array Functions
        Byte Array Functions
      • Converting Functions
        Converting Functions
      • Data Transaction Functions
        Data Transaction Functions
      • Decoding Functions
        Decoding Functions
      • Encoding Functions
        Encoding Functions
      • Exception Functions
        Exception Functions
      • Hashing Functions
        Hashing Functions
      • List Functions
        List Functions
      • Math Functions
        Math Functions
      • String Functions
        String Functions
      • Union Functions
        Union Functions
      • Verification Functions
        Verification Functions
      Built-in Functions
    • Operators
      Operators
    • Limitations
      • Data Weight
        Data Weight
      Limitations
    Previous Versions: 4 and 3
  • Import Libraries
    Import Libraries
  • Ride Components
    Ride Components
      • English
      • Русский
      On this page
        • fraction(Int, Int, Int): Int
        • fraction(Int, Int, Int, Union): Int
        • fraction(BigInt, BigInt, BigInt): BigInt
        • fraction(BigInt, BigInt, BigInt, Union): BigInt
        • log(Int, Int, Int, Int, Int, Union): Int
        • log(BigInt, Int, BigInt, Int, Int, Union): BigInt
        • median(List[Int]): Int
        • median(List[BigInt]): BigInt
        • pow(Int, Int, Int, Int, Int, Union): Int
        • pow(BigInt, Int, BigInt, Int, Int, Union): BigInt
        • sqrt(Int, Int, Int, Union): Int
        • sqrt(BigInt, Int, Int, Union): BigInt
        • Rounding Variables
      waves_logo Docs

          # [Ride v6] Math functions

          ⚠️ This is the documentation for the Standard Library version 6, which becomes available after activation of feature #17 “Ride V6, MetaMask support”. Go to version 5

          Name Description Complexity
          fraction(Int, Int, Int): Int Multiplies and divides integers to avoid overflow 1
          fraction(Int, Int, Int, Union): Int Multiplies and divides integers to avoid overflow, applying the specified rounding method 1
          fraction(BigInt, BigInt, BigInt): BigInt Multiplies and divides big integers to avoid overflow 1
          fraction(BigInt, BigInt, BigInt, Union): BigInt Multiplies and divides big integers to avoid overflow, applying the specified rounding method 1
          log(Int, Int, Int, Int, Int, Union): Int Calculates logarithm of a number to a given base 100
          log(BigInt, Int, BigInt, Int, Int, Union): BigInt Calculates logarithm of a number to a given base with high accuracy 200
          median(List[Int]): Int Returns the median of a list of integers 20
          median(List[BigInt]): BigInt Returns the median of a list of big integers 160
          pow(Int, Int, Int, Int, Int, Union): Int Raises a number to a given power 28
          pow(BigInt, Int, BigInt, Int, Int, Union): BigInt Raises a number to a given power with high accuracy 270
          sqrt(Int, Int, Int, Union): Int Returns the square root of a number 2
          sqrt(BigInt, Int, Int, Union): BigInt Returns the square root of a number with high accuracy 5

          # fraction(Int, Int, Int): Int

          Multiplies integers a, b and divides the result by the integer c to avoid overflow.

          Fraction a × b / c should not exceed the maximum value of the integer type 9,223,372,036,854,755,807.

          The rounding method is DOWN, see Rounding variables below.

          fraction(a: Int, b: Int, c: Int): Int
          

          # Parameters

          Parameter Description
          a: Int Integer a
          b: Int Integer b
          c: Int Integer c

          # Example

          Lets assume that:

          a = 100,000,000,000,

          b = 50,000,000,000,000,

          c = 2,500,000.

          The following formula, with operators * and /, fails due to overflow:

          a * b / c #  overflow, because a × b exceeds max integer value
          

          The fraction function with no overflow:

          fraction(a, b, c) # Result: 2,000,000,000,000,000,000
          

          # fraction(Int, Int, Int, Union): Int

          Multiplies integers a, b and divides the result by the integer c to avoid overflow, applying the specified rounding method.

          Fraction a × b / c should not exceed the maximum value of the integer type 9,223,372,036,854,755,807.

          fraction(a: Int, b: Int, c: Int, round: DOWN|CEILING|FLOOR|HALFUP|HALFEVEN): Int
          

          # Parameters

          Parameter Description
          a: Int Integer a
          b: Int Integer b
          c: Int Integer c
          round: DOWN|CEILING|FLOOR|HALFUP|HALFEVEN One of the rounding variables

          # fraction(BigInt, BigInt, BigInt): BigInt

          Multiplies big integers a, b and divides the result by the integer c to avoid overflow.

          Fraction a × b / c should not exceed the maximum value of the big integer type.

          The rounding method is DOWN, see Rounding variables below.

          fraction(a: BigInt, b: BigInt, c: BigInt): BigInt
          

          # Parameters

          Parameter Description
          a: BigInt Big integer a
          b: BigInt Big integer b
          c: BigInt Big integer c

          # fraction(BigInt, BigInt, BigInt, Union): BigInt

          Multiplies big integers a, b and divides the result by the integer c to avoid overflow, applying the specified rounding method.

          Fraction a × b / c should not exceed the maximum value of the big integer type.

          fraction(a: BigInt, b: BigInt, c: BigInt, round: DOWN|CEILING|FLOOR|HALFUP|HALFEVEN): BigInt
          

          # Parameters

          Parameter Description
          a: BigInt Big integer a
          b: BigInt Big integer b
          c: BigInt Big integer c
          round: DOWN|CEILING|FLOOR|HALFUP|HALFEVEN One of the rounding variables

          # log(Int, Int, Int, Int, Int, Union): Int

          Calculates logba.

          log(value: Int, vp: Int, base: Int, bp: Int, rp: Int, round: DOWN|CEILING|FLOOR|HALFUP|HALFEVEN): Int
          

          In Ride, there is no data type with the floating point. That is why, for example, when you need to calculate log2.716.25 then the number value = 1625, vp = 2 and the base = 27, bp = 1.

          More examples:

          a value vp
          16.25 1625 2
          5 5 0
          5.00 500 2

          If the log function returns, for example, 2807, and the parameter rp = 3, then the result is 2.807; in the number 2807 the last 3 digits is a fractional part.

          # Parameters

          Parameter Description
          value: Int Number a without decimal point
          vp: Int Number of decimals of a, from 0 to 8 inclusive
          base: Int Logarithm base b without decimal point
          bp: Int Number of decimals of b, from 0 to 8 inclusive
          rp: Int Number of decimals in the resulting value, from 0 to 8 inclusive. Specifies the accuracy of the calculated result
          round: DOWN|CEILING|FLOOR|HALFUP|HALFEVEN One of the rounding variables

          # Examples

          log2.716.25 = 2.807035421...

          log(1625, 2, 27, 1, 2, HALFUP) # Function returns 281, so the result is: 2.81
          log(1625, 2, 27, 1, 5, HALFUP) # Function returns 280703542, so the result is: 2.80704
          
          log(0, 0, 2, 0, 0, HALFUP)     # Result: -Infinity
          

          # log(BigInt, Int, BigInt, Int, Int, Union): BigInt

          Calculates logba with high accuracy.

          log(value: BigInt, ep: Int, base: BigInt, bp: Int, rp: Int, round: DOWN|CEILING|FLOOR|HALFUP|HALFEVEN): BigInt
          

          In Ride, there is no data type with the floating point. That is why, for example, when you need to calculate log2.716.25 then value = 1625, vp = 2 and the base = 27, bp = 1.

          If the log function returns, for example, 2807035420964590265, and the parameter rp = 18, then the result is 2.807035420964590265; in the number 2807035420964590265 the last 18 digits is a fractional part.

          # Parameters

          Parameter Description
          value: BigInt Number a without decimal point
          vp: Int Number of decimals of a, from 0 to 18 inclusive
          base: BigInt Logarithm base b without decimal point
          bp: Int Number of decimals of b, from 0 to 18 inclusive
          rp: Int Number of decimals in the resulting value, from 0 to 18 inclusive. Specifies the accuracy of the calculated result
          round: DOWN|CEILING|FLOOR|HALFUP|HALFEVEN One of the rounding variables

          # median(List[Int]): Int

          Returns the median of the list of integers. Fails if the list is empty.

          median(arr: List[Int]): Int
          

          # Parameters

          Parameter Description
          arr: List[Int] List of integers

          # Examples

          median([1, 2, 3])         # Returns 2
          median([2, 4, 9, 20])     # Returns 6
          median([-2, -4, -9, -20]) # Returns -7
          

          # median(List[BigInt]): BigInt

          Returns the median of a list of big integers. Fails if the list is empty or contains more than 100 elements.

          median(arr: List[BigInt]): BigInt
          

          # Parameters

          Parameter Description
          arr: List[BigInt] List of big integers

          # pow(Int, Int, Int, Int, Int, Union): Int

          Calculates ab.

          pow(base: Int, bp: Int, exponent: Int, ep: Int, rp: Int, round: DOWN|CEILING|FLOOR|HALFUP|HALFEVEN): Int
          

          In Ride, there is no data type with the floating point. That is why, for example, when you need to calculate 16.252.7, then the number base = 1625, bp = 2, and the exponent = 27, ep = 1.

          If the pow function returns, for example, 18591057, and the parameter rp = 4, then the result is 1859.1057; in the number 18591057 the last 4 digits is a fractional part.

          # Parameters

          Parameter Description
          base: Int Number a without decimal point
          bp: Int Number of decimals of a, from 0 to 8 inclusive
          exponent: Int Exponent b without decimal point
          ep: Int Number of decimals of b, from 0 to 8 inclusive
          rp: Int Number of decimals in the resulting value, from 0 to 8 inclusive. Specifies the accuracy of the calculated result
          round: DOWN|CEILING|FLOOR|HALFUP|HALFEVEN One of the rounding variables

          # Examples

          16.252.7 = 1859,1057168...

          pow(1625, 2, 27, 1, 2, HALFUP) # function returns 185911, so the result is: 1859.11
          pow(1625, 2, 27, 1, 5, HALFUP) # function returns 185910572, so, the result is: 1859.10572
          

          # pow(BigInt, Int, BigInt, Int, Int, Union): BigInt

          Calculates ab with high accuracy.

          pow(base: BigInt, bp: Int, exponent: BigInt, ep: Int, rp: Int, round: DOWN|CEILING|FLOOR|HALFUP|HALFEVEN): BigInt
          

          In Ride, there is no data type with the floating point. That is why, for example, when you need to calculate 16.252.7, then the number base = 1625, bp = 2, and the exponent = 27, ep = 1.

          If the pow function returns, for example, 1859105716849757217692, and the parameter rp = 18, then the result is 1859.105716849757217692; in the number 1859105716849757217692 the last 18 digits is a fractional part.

          # Parameters

          Parameter Description
          base: BigInt Number a without decimal point
          bp: Int Number of decimals of a, from 0 to 18 inclusive
          exponent: BigInt Exponent b without decimal point
          ep: Int Number of decimals of b, from 0 to 18 inclusive
          rp: Int Number of decimals in the resulting value, from 0 to 18 inclusive. Specifies the accuracy of the calculated result
          round: DOWN|CEILING|FLOOR|HALFUP|HALFEVEN One of the rounding variables

          # sqrt(Int, Int, Int, Union): Int

          Calculates the square root of a number: √a.

          sqrt(base: Int, bp: Int, rp: Int, round: DOWN|CEILING|FLOOR|HALFUP|HALFEVEN): Int
          

          In Ride, there is no data type with the floating point. That is why, for example, when you need to calculate √16.25, specify base = 1625 and bp = 2.

          If the sqrt function returns, for example, 40311, and the parameter rp = 4, then the result is 4.0311; in the number 40311 the last 4 digits is a fractional part.

          # Parameters

          Parameter Description
          base: Int Number a without decimal point
          bp: Int Number of decimals of a
          rp: Int Number of decimals in the resulting value, from 0 to 8 inclusive. Specifies the accuracy of the calculated result
          round: DOWN|CEILING|FLOOR|HALFUP|HALFEVEN One of the rounding variables

          # Examples

          √16,25 = 4,03112887...

          sqrt(1625, 2, 2, HALFUP) # function returns 403, so the result is 4.03
          sqrt(1625, 2, 5, HALFUP) # function returns 403113, so the result is 4.03113
          

          # sqrt(BigInt, Int, Int, Union): BigInt

          Calculates the square root of a number with high accuracy.

          sqrt(base: BigInt, bp: Int, rp: Int, round: DOWN|CEILING|FLOOR|HALFUP|HALFEVEN): BigInt
          

          In Ride, there is no data type with the floating point. That is why, for example, when you need to calculate √16.25, specify base = 1625 and bp = 2.

          If the sqrt function returns, for example, 4031128874149274826, and the parameter rp = 18, then the result is 4.031128874149274826; in the number 4031128874149274826 the last 18 digits is a fractional part.

          # Parameters

          Parameter Description
          base: BigInt Number a without decimal point
          bp: Int Number of decimals of a
          rp: Int Number of decimals in the resulting value, from 0 to 18 inclusive. Specifies the accuracy of the calculated result
          round: DOWN|CEILING|FLOOR|HALFUP|HALFEVEN One of the rounding variables

          # Rounding Variables

          Below is the list of built-in rounding variables. Every variable corresponds to the rounding method (opens new window).

          The rounding variables are only used as the parameters of functions fraction, log, pow.

          Name Description
          DOWN Rounds towards zero
          CEILING Rounds towards positive infinity
          FLOOR Rounds towards negative infinity
          HALFUP Rounds towards the nearest integer; if the integers are equidistant, then rounds away from zero
          HALFEVEN Rounds towards the nearest integer; if the integers are equidistant, then rounds towards the nearest even integer

          # Examples

          Input number/Rounding method DOWN CEILING FLOOR HALFUP HALFEVEN
          5.5 5 6 5 6 6
          2.5 2 3 2 3 2
          1.6 1 2 1 2 2
          1.1 1 2 1 1 1
          1.0 1 1 1 1 1
          -1.0 -1 -1 -1 -1 -1
          -1.1 -1 -1 -2 -1 -1
          -1.6 -1 -1 -2 -2 -2
          -2.5 -2 -2 -3 -3 -2
          -5.5 -5 -5 -6 -6 -6
          Hashing Functions
          String Functions
          Hashing Functions
          String Functions