Home | History | Annotate | Line # | Download | only in libm
t_round.c revision 1.3
      1  1.3  jruoho /* $NetBSD: t_round.c,v 1.3 2011/09/13 08:58:41 jruoho Exp $ */
      2  1.3  jruoho 
      3  1.3  jruoho /*-
      4  1.3  jruoho  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  1.3  jruoho  * All rights reserved.
      6  1.3  jruoho  *
      7  1.3  jruoho  * Redistribution and use in source and binary forms, with or without
      8  1.3  jruoho  * modification, are permitted provided that the following conditions
      9  1.3  jruoho  * are met:
     10  1.3  jruoho  * 1. Redistributions of source code must retain the above copyright
     11  1.3  jruoho  *    notice, this list of conditions and the following disclaimer.
     12  1.3  jruoho  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.3  jruoho  *    notice, this list of conditions and the following disclaimer in the
     14  1.3  jruoho  *    documentation and/or other materials provided with the distribution.
     15  1.3  jruoho  *
     16  1.3  jruoho  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.3  jruoho  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.3  jruoho  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.3  jruoho  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.3  jruoho  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.3  jruoho  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.3  jruoho  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.3  jruoho  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.3  jruoho  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.3  jruoho  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.3  jruoho  * POSSIBILITY OF SUCH DAMAGE.
     27  1.3  jruoho  */
     28  1.1  jruoho 
     29  1.1  jruoho #include <atf-c.h>
     30  1.1  jruoho #include <math.h>
     31  1.1  jruoho 
     32  1.1  jruoho /*
     33  1.1  jruoho  * This tests for a bug in the initial implementation where
     34  1.1  jruoho  * precision was lost in an internal substraction, leading to
     35  1.1  jruoho  * rounding into the wrong direction.
     36  1.1  jruoho  */
     37  1.1  jruoho 
     38  1.1  jruoho /* 0.5 - EPSILON */
     39  1.1  jruoho #define VAL	0x0.7ffffffffffffcp0
     40  1.1  jruoho #define VALF	0x0.7fffff8p0
     41  1.1  jruoho 
     42  1.2     mrg #ifdef __vax__
     43  1.2     mrg #define SMALL_NUM	1.0e-38
     44  1.2     mrg #else
     45  1.2     mrg #define SMALL_NUM	1.0e-40
     46  1.2     mrg #endif
     47  1.2     mrg 
     48  1.1  jruoho ATF_TC(round_dir);
     49  1.1  jruoho ATF_TC_HEAD(round_dir, tc)
     50  1.1  jruoho {
     51  1.1  jruoho 	atf_tc_set_md_var(tc, "descr","Check for rounding in wrong direction");
     52  1.1  jruoho }
     53  1.1  jruoho 
     54  1.1  jruoho ATF_TC_BODY(round_dir, tc)
     55  1.1  jruoho {
     56  1.1  jruoho 	double a = VAL, b, c;
     57  1.1  jruoho 	float af = VALF, bf, cf;
     58  1.1  jruoho 
     59  1.1  jruoho 	b = round(a);
     60  1.1  jruoho 	bf = roundf(af);
     61  1.1  jruoho 
     62  1.3  jruoho 	ATF_CHECK(fabs(b) < SMALL_NUM);
     63  1.3  jruoho 	ATF_CHECK(fabsf(bf) < SMALL_NUM);
     64  1.1  jruoho 
     65  1.1  jruoho 	c = round(-a);
     66  1.1  jruoho 	cf = roundf(-af);
     67  1.1  jruoho 
     68  1.3  jruoho 	ATF_CHECK(fabs(c) < SMALL_NUM);
     69  1.3  jruoho 	ATF_CHECK(fabsf(cf) < SMALL_NUM);
     70  1.1  jruoho }
     71  1.1  jruoho 
     72  1.1  jruoho ATF_TP_ADD_TCS(tp)
     73  1.1  jruoho {
     74  1.1  jruoho 
     75  1.1  jruoho 	ATF_TP_ADD_TC(tp, round_dir);
     76  1.1  jruoho 
     77  1.1  jruoho 	return atf_no_error();
     78  1.1  jruoho }
     79