s_roundf.c (5792e54aa9b474a676b89031101c11a7009d0238) | s_roundf.c (d0f136337018033643078ec257de0ad091bcaec8) |
---|---|
1/*- 2 * Copyright (c) 2003, Steven G. Kargl 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 19 unchanged lines hidden (view full) --- 28__FBSDID("$FreeBSD$"); 29 30#include <math.h> 31 32float 33roundf(float x) 34{ 35 float t; | 1/*- 2 * Copyright (c) 2003, Steven G. Kargl 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 19 unchanged lines hidden (view full) --- 28__FBSDID("$FreeBSD$"); 29 30#include <math.h> 31 32float 33roundf(float x) 34{ 35 float t; |
36 int i; |
|
36 | 37 |
37 if (!isfinite(x)) | 38 i = fpclassify(x); 39 if (i == FP_INFINITE || i == FP_NAN) |
38 return (x); 39 40 if (x >= 0.0) { | 40 return (x); 41 42 if (x >= 0.0) { |
41 t = floorf(x); 42 if (t - x <= -0.5) 43 t += 1.0; | 43 t = ceilf(x); 44 if (t - x > 0.5) 45 t -= 1.0; |
44 return (t); 45 } else { | 46 return (t); 47 } else { |
46 t = floorf(-x); 47 if (t + x <= -0.5) 48 t += 1.0; | 48 t = ceilf(-x); 49 if (t + x > 0.5) 50 t -= 1.0; |
49 return (-t); 50 } 51} | 51 return (-t); 52 } 53} |