| 1 | // |
| 2 | // SPDX-License-Identifier: BSD-3-Clause |
| 3 | // Copyright (c) Contributors to the OpenEXR Project. |
| 4 | // |
| 5 | |
| 6 | #ifndef INCLUDED_IMF_RATIONAL_H |
| 7 | #define INCLUDED_IMF_RATIONAL_H |
| 8 | |
| 9 | #include "ImfExport.h" |
| 10 | #include "ImfNamespace.h" |
| 11 | |
| 12 | //----------------------------------------------------------------------------- |
| 13 | // |
| 14 | // Rational numbers |
| 15 | // |
| 16 | // A rational number is represented as pair of integers, n and d. |
| 17 | // The value of of the rational number is |
| 18 | // |
| 19 | // n/d for d > 0 |
| 20 | // positive infinity for n > 0, d == 0 |
| 21 | // negative infinity for n < 0, d == 0 |
| 22 | // not a number (NaN) for n == 0, d == 0 |
| 23 | // |
| 24 | //----------------------------------------------------------------------------- |
| 25 | |
| 26 | OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER |
| 27 | |
| 28 | |
| 29 | class IMF_EXPORT_TYPE Rational |
| 30 | { |
| 31 | public: |
| 32 | |
| 33 | int n; // numerator |
| 34 | unsigned int d; // denominator |
| 35 | |
| 36 | |
| 37 | //---------------------------------------- |
| 38 | // Default constructor, sets value to zero |
| 39 | //---------------------------------------- |
| 40 | |
| 41 | Rational (): n (0), d (1) {} |
| 42 | |
| 43 | |
| 44 | //------------------------------------- |
| 45 | // Constructor, explicitly sets n and d |
| 46 | //------------------------------------- |
| 47 | |
| 48 | Rational (int n, int d): n (n), d (d) {} |
| 49 | |
| 50 | |
| 51 | //---------------------------- |
| 52 | // Constructor, approximates x |
| 53 | //---------------------------- |
| 54 | |
| 55 | IMF_EXPORT |
| 56 | explicit Rational (double x); |
| 57 | |
| 58 | |
| 59 | //--------------------------------- |
| 60 | // Approximate conversion to double |
| 61 | //--------------------------------- |
| 62 | |
| 63 | operator double () const {return double (n) / double (d);} |
| 64 | }; |
| 65 | |
| 66 | |
| 67 | OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT |
| 68 | |
| 69 | #endif |
| 70 | |