blob: 04a9354f3b6f2992315d9c920122ab6cd251db62 [file] [log] [blame]
Kai Tietz815a6642009-02-19 10:54:09 +00001/**
2 * This file has no copyright assigned and is placed in the Public Domain.
Rafaël Carré8a67ab42012-06-28 15:40:59 +00003 * This file is part of the mingw-w64 runtime package.
Kai Tietzfa0cfe32010-01-15 20:02:21 +00004 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
Kai Tietz815a6642009-02-19 10:54:09 +00005 */
6#include <math.h>
7#include <limits.h>
8#include <errno.h>
9
10long
11lroundf (float x)
12{
13 float res;
14
15 if (x >= 0.0F)
16 {
17 res = ceilf (x);
18 if (res - x > 0.5F)
19 res -= 1.0F;
20 }
21 else
22 {
23 res = ceilf (-x);
24 if (res + x > 0.5F)
25 res -= 1.0F;
26 res = -res;
27 }
28 if (!isfinite (res)
29 || res > (float) LONG_MAX
30 || res < (float) LONG_MIN)
31 {
32 errno = ERANGE;
33 /* Undefined behaviour, so we could return anything. */
34 /* return res > 0.0F ? LONG_MAX : LONG_MIN; */
35 }
36 return (long) res;
Ozkan Sezer8e8a03d2009-08-30 08:35:43 +000037}