File: simplefloat.c

package info (click to toggle)
sdcc 4.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 105,232 kB
  • sloc: ansic: 956,095; cpp: 110,511; makefile: 59,314; sh: 29,875; asm: 17,178; perl: 12,136; yacc: 7,480; lisp: 1,672; python: 907; lex: 805; awk: 498; sed: 89
file content (88 lines) | stat: -rw-r--r-- 1,618 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/** Simple set of tests for floating pt.
 */
#include <testfwk.h>
#include <math.h>

#if (PORT_HOST)
#  define FCOMP(a,b) (fabsf((a) - (b)) < ((b) * 1e-7))
#else
   /* Testing floats for equality is normally a bug,
      but too keep this test simple we dare it. And
      it works with the exception of the division on
      the host port. */
#  define FCOMP(a,b) ((a) == (b))
#endif

void
testCmp (void)
{
#if !defined( __SDCC_pdk14) && !defined( __SDCC_pdk15) // Lack of memory
  volatile float left, right;

  left = 5;
  right = 13;
  ASSERT (left + right == 18);
  ASSERT (left + right <= 18);
  ASSERT (left + right >= 18);
  ASSERT (left + right > 17.9);
  ASSERT (left + right < 18.1);
#endif
}

void
testDiv (void)
{
#if !defined( __SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
#if defined (__SDCC_mcs51) && !defined (__SDCC_STACK_AUTO)
  __idata __at 0xd0
#endif
  volatile float left;
  volatile float right;

  left = 17;
  right = 343;

  ASSERT (FCOMP (left / right, (17.0 / 343.0)));
  ASSERT (FCOMP (right / left, (343.0 / 17.0)));

  right = 17;
  ASSERT (FCOMP (left / right, 1.0));
#endif
}

void
testDivNearOne (void)
{
#if !defined( __SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
  volatile float left, right, result;

  left = 12392.4;
  right = 12392.4;
  result = left / right;

  if (result > 0.999999)
    {
      /* Fine */
    }
  else
    {
      FAIL ();
    }
  if (result < 1.00001)
    {
      /* Fine */
    }
  else
    {
      FAIL ();
    }
  if (result > 0.999999 && result < 1.00001)
    {
      /* Fine */
    }
  else
    {
      FAIL ();
    }
#endif
}