Code Monkey home page Code Monkey logo

clamp's Introduction

clamp

Adds the support of the mathematical method clamp() for PHP.

Badge Packagist Version Packagist PHP Version GitHub License GitHub Actions Workflow Status Code Climate coverage Code Climate maintainability

Installation

The recommended way of installing Larvatar is to use Composer. Run the following command to install it to you project:

composer require renfordt/clamp

Usage

The usage is very simple and comparable to the C++ function:

clamp(
    $value, // The value to be clamped
    $min, // The minimum value to clamp to
    $max // The maximum value to clamp to
);

Alternatively you can use clampMinMax() which is a bit slower.

clampMinMax(
    $value, // The value to be clamped
    $min, // The minimum value to clamp to
    $max // The maximum value to clamp to
);

Why another package?

Even though there are some similar packages, this one focuses on different approaches.

First of all the syntax is similar to c++ clamp function.

Secondly and more important this package focuses on performance. Other packages uses the max($min, min($max, $num)) approach but this packages works with the following code:

if ($value > $max) {
    return $max;
} elseif ($value < $min) {
    return $min;
}
return $value;

Even though the readability is a bit worse, the performance is up to 2x faster. In most cases this is not noticeable but in some cases there will be a benefit.

Over a iteration of 100.000 executions the functions need the following times:

String

  • clamp: 0.0035040378570557 sec
  • clampMinMax: 0.0061681270599365 sec

Integer

  • clamp: 0.0029380321502686 sec
  • clampMinMax: 0.0056021213531494 sec

Float

  • clamp: 0.0028560161590576 sec
  • clampMinMax: 0.0062460899353027 sec

clamp's People

Contributors

renfordt 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.