~ubuntu-branches/debian/sid/genius/sid

« back to all changes in this revision

Viewing changes to mpfr/tests/tsgn.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-08-21 12:57:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060821125745-sl9ks8v7fq324bdf
Tags: upstream-0.7.6.1
ImportĀ upstreamĀ versionĀ 0.7.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* tsgn -- Test for the sign of a floating point number.
 
2
 
 
3
Copyright 2003 Free Software Foundation.
 
4
Contributed by the Spaces project, INRIA Lorraine.
 
5
 
 
6
This file is part of the MPFR Library.
 
7
 
 
8
The MPFR Library is free software; you can redistribute it and/or modify
 
9
it under the terms of the GNU Lesser General Public License as published by
 
10
the Free Software Foundation; either version 2.1 of the License, or (at your
 
11
option) any later version.
 
12
 
 
13
The MPFR Library is distributed in the hope that it will be useful, but
 
14
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
15
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
16
License for more details.
 
17
 
 
18
You should have received a copy of the GNU Lesser General Public License
 
19
along with the MPFR Library; see the file COPYING.LIB.  If not, write to
 
20
the Free Software Foundation, Inc., 51 Franklin Place, Fifth Floor, Boston,
 
21
MA 02110-1301, USA. */
 
22
 
 
23
#include <stdio.h>
 
24
#include <stdlib.h>
 
25
 
 
26
#include "mpfr-test.h"
 
27
 
 
28
static void
 
29
check_special(void)
 
30
{
 
31
  mpfr_t x;
 
32
  int ret = 0;
 
33
 
 
34
  mpfr_init(x);
 
35
  MPFR_SET_ZERO(x);
 
36
  if ((mpfr_sgn) (x) != 0)
 
37
    {
 
38
      printf("Sgn error for 0.\n");
 
39
      ret = 1;
 
40
    }
 
41
  MPFR_SET_INF(x);
 
42
  MPFR_SET_POS(x);
 
43
  if ((mpfr_sgn) (x) != 1)
 
44
    {
 
45
      printf("Sgn error for +Inf.\n");
 
46
      ret = 1;
 
47
    }
 
48
  MPFR_SET_INF(x);
 
49
  MPFR_SET_NEG(x);
 
50
  if ((mpfr_sgn) (x) != -1)
 
51
    {
 
52
      printf("Sgn error for -Inf.\n");
 
53
      ret = 1;
 
54
    }
 
55
  mpfr_clear(x);
 
56
  if (ret)
 
57
    exit(ret);
 
58
}
 
59
 
 
60
static void
 
61
check_sgn(void)
 
62
{
 
63
  mpfr_t x;
 
64
  int i, s1, s2;
 
65
 
 
66
  mpfr_init(x);
 
67
  for(i = 0 ; i < 100 ; i++)
 
68
    {
 
69
      mpfr_random(x);
 
70
      if (i&1)
 
71
        {
 
72
          MPFR_SET_POS(x);
 
73
          s2 = 1;
 
74
        }
 
75
      else
 
76
        {
 
77
          MPFR_SET_NEG(x);
 
78
          s2 = -1;
 
79
        }
 
80
      s1 = mpfr_sgn(x);
 
81
      if (s1 < -1 || s1 > 1)
 
82
        {
 
83
          printf("Error for sgn: out of range.\n");
 
84
          goto lexit;
 
85
        }
 
86
      else if (MPFR_IS_NAN(x) || MPFR_IS_ZERO(x))
 
87
        {
 
88
          if (s1 != 0)
 
89
            {
 
90
              printf("Error for sgn: Nan or Zero should return 0.\n");
 
91
              goto lexit;
 
92
            }
 
93
        }
 
94
      else if (s1 != s2)
 
95
        {
 
96
          printf("Error for sgn. Return %d instead of %d.\n", s1, s2);
 
97
          goto lexit;
 
98
        }
 
99
    }
 
100
  mpfr_clear(x);
 
101
  return;
 
102
 
 
103
 lexit:
 
104
  mpfr_clear(x);
 
105
  exit(1);
 
106
}
 
107
 
 
108
int
 
109
main (int argc, char *argv[])
 
110
{
 
111
  tests_start_mpfr ();
 
112
 
 
113
  check_special ();
 
114
  check_sgn ();
 
115
 
 
116
  tests_end_mpfr ();
 
117
  return 0;
 
118
}