/solidity-trigonometry

Solidity library with basic trigonometry functions

Primary LanguageSolidityMIT LicenseMIT

Solidity Trigonometry

Solidity library offering basic trigonometry functions where inputs and outputs are integers. Inputs are specified in radians scaled by 1e18, and similarly outputs are scaled by 1e18. Each invocation of the sin() and cos() functions cost around 5000–5600 gas (see .gas-snapshot) for more info.

This implementation is based off the Solidity trigonometry library written by Lefteris Karapetsas which can be found here. Compared to Lefteris' implementation, this version makes the following changes:

  • Uses a 32 bits instead of 16 bits for improved accuracy
  • Updated for Solidity 0.8.x
  • Various gas optimizations
  • Change inputs/outputs to standard trig format (scaled by 1e18) instead of requiring the integer format used by the algorithm

The original implementation by Lefteris is based off Dave Dribin's trigint C library, which in turn is based on an article by Scott Dattalo.

Usage

To use this in a dapptools project, install it with:

dapp install https://github.com/mds1/solidity-trigonometry

There is currently no npm package, so for projects using Hardhat or other development frameworks, use:

yarn add https://github.com/mds1/solidity-trigonometry.git

Development

This library is developed with dapptools.

To update gas snapshots:

  1. Install duppgrade
  2. Switch dapptools to the WIP branch with gas snapshot functionality with duppgrade dapphub/dapptools gas-snapshot-dev
  3. Run dapp --snapshot