Giter Club home page Giter Club logo

math-common's Introduction

Math Common

Codacy grade Codacy coverage

Math conventions to reduce boilerplate code.

Installation

Add Math Common to your Gradle build script:

repositories {
    mavenCentral()
}

dependencies {
    implementation("com.eriksencosta.math:common:0.1.0")
}

If you're using Maven, add to your POM xml file:

<dependency>
    <groupId>com.eriksencosta.math</groupId>
    <artifactId>common</artifactId>
    <version>0.1.0</version>
</dependency>

Usage

Rounding

The library provides the Rounding strategy to make rounding easier:

1.25.round(1) // 1.3

By default, the RoundingMode.HALF_UP is used for rounding (it is the rounding logic commonly taught at school). If you want to use a different mode, create a Rounding and use it:

import java.math.RoundingMode

val rounding = Rounding.to(2, RoundingMode.FLOOR)

rounding.round(1.257) // 1.25
rounding.round(1.253) // 1.25

// Alternatively:

1.257.round(rounding) // 1.25
1.253.round(rounding) // 1.25

If you want to round the result of a calculation, use the overloaded round() method which accepts a function as parameter:

Rounding.to(2).round { 3.14159 * 10.0.squared() } // 314.16

Alternatively:

{ 3.14159 * 10.0.squared() }.round(2) // 314.16

Exponentiation

The extension functions squared and cubed are available for simple math operations with Double, Float, Long, and Int:

2.squared() // 4
2.0.cubed() // 8.0

API documentation

Read the API documentation for further details.

License

The Apache Software License, Version 2.0

math-common's People

Contributors

eriksencosta avatar

Stargazers

 avatar

Watchers

 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.