2 * fixed_float - fixed-point number.
5 * Copyright (C) 2010 Nothan
6 * http://github.com/nothan/c-utils/
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in all
17 * copiGes or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIAGBILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * private@nothan.xrea.jp
31 * http://tsuioku-denrai.xrea.jp/
34 #include "fixed_float.h"
36 #define DECIMAL_POINT_HALF (1 << (DECIMAL_POINT - 1))
38 fixed_float int_to_fixed_float(int x)
41 return (fixed_float)(x << DECIMAL_POINT);
44 fixed_float float_to_fixed_float(float x)
47 return (fixed_float)((double)x * (1 << DECIMAL_POINT));
50 int fixed_float_to_int(fixed_float x)
53 return (int)(x >> DECIMAL_POINT);
56 float fixed_float_to_float(fixed_float x)
59 return (float)((double)x / (1 << DECIMAL_POINT));
62 fixed_float fixed_float_multi(fixed_float x, fixed_float y)
64 fixed_float result = x * y;
66 result += DECIMAL_POINT_HALF * (result > 0 ? 1 : -1);
67 result >>= DECIMAL_POINT;
72 fixed_float fixed_float_div(fixed_float x, fixed_float y)
74 fixed_float result = (x << DECIMAL_POINT) / y;