[PATCH 02/27] Input: Add round_towards_zero helper
walter harms
wharms at bfs.de
Fri Jun 3 09:04:34 PDT 2011
Hello daniel,
C99 defines FE_TOWARDZERO, so any compiler that support c99 should work
with the example below.
re,
wh
ntl: i guess you code will work also with older compilers
#include <stdio.h>
#include <fenv.h>
#include <math.h>
/* gcc fesetround.c -lm -std=c99 */
int main()
{
double ii;
fesetround(FE_TOWARDZERO);
for(ii=-2.0;ii<2.0;ii+=0.2)
printf("%f,nextint=%f\n",ii,nearbyint(ii));
return 0;
}
Am 03.06.2011 16:59, schrieb Daniel Stone:
> lrint() exists to round _away_ from zero, but there's no equivalent for
> rounding towards zero, aside from tediously calling fesetenv(), which
> isn't hugely portable anyway, then lround(). Great.
>
> Signed-off-by: Daniel Stone <daniel at fooishbar.org>
> ---
> dix/inpututils.c | 9 +++++++++
> include/input.h | 2 ++
> 2 files changed, 11 insertions(+), 0 deletions(-)
>
> diff --git a/dix/inpututils.c b/dix/inpututils.c
> index 49e1758..bf8d093 100644
> --- a/dix/inpututils.c
> +++ b/dix/inpututils.c
> @@ -584,3 +584,12 @@ void verify_internal_event(const InternalEvent *ev)
> FatalError("Wrong event type %d. Aborting server\n", ev->any.header);
> }
> }
> +
> +double
> +round_towards_zero(double val)
> +{
> + if (val >= 0.0)
> + return floor(val);
> + else
> + return ceil(val);
> +}
> diff --git a/include/input.h b/include/input.h
> index 56847ed..a15623a 100644
> --- a/include/input.h
> +++ b/include/input.h
> @@ -594,4 +594,6 @@ extern _X_EXPORT void valuator_mask_copy(ValuatorMask *dest,
> const ValuatorMask *src);
> extern _X_EXPORT int valuator_mask_get(const ValuatorMask *mask, int valnum);
>
> +extern _X_EXPORT double round_towards_zero(double val);
> +
> #endif /* INPUT_H */
More information about the xorg-devel
mailing list