[go: up one dir, main page]

File: Float.py

package info (click to toggle)
uranium 3.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,876 kB
  • sloc: python: 22,349; sh: 111; makefile: 11
file content (35 lines) | stat: -rw-r--r-- 1,326 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Copyright (c) 2015 Ultimaker B.V.
# Uranium is released under the terms of the LGPLv3 or higher.


##  Class containing helper functions for dealing with IEEE-754 floating point numbers.
#
#
class Float:
    ##  Compare two floats to check if they are equal with a tolerance value.
    #
    #   This method will compare two floats and check whether they are equal to
    #   within a certain tolerance value.
    #
    #   \param f1 \type{float} The first value to compare.
    #   \param f2 \type{float} The second value to compare.
    #   \param tolerance \type{float} The amount of tolerance used to consider the two numbers "equal".
    #
    #   \return True if the two numbers are considered equal, False if not.
    @staticmethod
    def fuzzyCompare(f1, f2, tolerance = 1e-8):
        if f1 == f2:
            return True

        return f1 > (f2 - tolerance) and f1 < (f2 + tolerance)

    ##  Return the value clamped to a minimum and maximum value.
    #
    #   \param f1 \type{float} The value to clamp.
    #   \param minimum \type{float} The minimum value.
    #   \param maximum \type{float} The maximum value.
    #
    #   \return \type{float} Minimum if f1 < minimum, maximum if f1 > maximum, else f1.
    @staticmethod
    def clamp(f1, minimum, maximum):
        return min(max(f1, minimum), maximum)