[992ffa6] | 1 | /*
|
---|
| 2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
| 3 | * Copyright (c) 2014 Martin Decky
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup libmath
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <math.h>
|
---|
[516e780] | 37 | #include <float.h>
|
---|
| 38 | #include <stdint.h>
|
---|
[992ffa6] | 39 |
|
---|
[516e780] | 40 | /**
|
---|
| 41 | * Rounds its argument to the nearest integer value in floating-point format,
|
---|
| 42 | * rounding halfway cases away from zero, regardless of the current rounding
|
---|
| 43 | * direction.
|
---|
[992ffa6] | 44 | */
|
---|
[516e780] | 45 | float roundf(float val)
|
---|
[992ffa6] | 46 | {
|
---|
[516e780] | 47 | const int exp_bias = FLT_MAX_EXP - 1;
|
---|
| 48 | const int mant_bits = FLT_MANT_DIG - 1;
|
---|
| 49 |
|
---|
| 50 | union {
|
---|
| 51 | float f;
|
---|
| 52 | uint32_t i;
|
---|
| 53 | } u = { .f = fabsf(val) };
|
---|
| 54 |
|
---|
| 55 | int exp = (u.i >> mant_bits) - exp_bias;
|
---|
| 56 |
|
---|
| 57 | /* If value is less than 0.5, return zero with appropriate sign. */
|
---|
| 58 | if (exp < -1)
|
---|
| 59 | return copysignf(0.0f, val);
|
---|
| 60 |
|
---|
| 61 | /* If exponent is exactly mant_bits, adding 0.5 could change the result. */
|
---|
| 62 | if (exp >= mant_bits)
|
---|
| 63 | return val;
|
---|
| 64 |
|
---|
| 65 | /* Use trunc with adjusted value to do the rounding. */
|
---|
| 66 | return copysignf(truncf(u.f + 0.5), val);
|
---|
[992ffa6] | 67 | }
|
---|
| 68 |
|
---|
[516e780] | 69 | /**
|
---|
| 70 | * Rounds its argument to the nearest integer value in floating-point format,
|
---|
| 71 | * rounding halfway cases away from zero, regardless of the current rounding
|
---|
| 72 | * direction.
|
---|
[992ffa6] | 73 | */
|
---|
[516e780] | 74 | double round(double val)
|
---|
[992ffa6] | 75 | {
|
---|
[516e780] | 76 | const int exp_bias = DBL_MAX_EXP - 1;
|
---|
| 77 | const int mant_bits = DBL_MANT_DIG - 1;
|
---|
| 78 |
|
---|
| 79 | union {
|
---|
| 80 | double f;
|
---|
| 81 | uint64_t i;
|
---|
| 82 | } u = { .f = fabs(val) };
|
---|
| 83 |
|
---|
| 84 | int exp = ((int)(u.i >> mant_bits)) - exp_bias;
|
---|
| 85 |
|
---|
| 86 | /* If value is less than 0.5, return zero with appropriate sign. */
|
---|
| 87 | if (exp < -1)
|
---|
| 88 | return copysign(0.0, val);
|
---|
| 89 |
|
---|
| 90 | /* If exponent is exactly mant_bits, adding 0.5 could change the result. */
|
---|
| 91 | if (exp >= mant_bits)
|
---|
| 92 | return val;
|
---|
| 93 |
|
---|
| 94 | /* Use trunc with adjusted value to do the rounding. */
|
---|
| 95 | return copysign(trunc(u.f + 0.5), val);
|
---|
[992ffa6] | 96 | }
|
---|
| 97 |
|
---|
| 98 | /** @}
|
---|
| 99 | */
|
---|