-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathfloats.c
74 lines (59 loc) · 1.59 KB
/
floats.c
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
#include <math.h>
#include <assert.h>
float g = 3.5f;
float f_array[4] = { 1.0f, 2.0f, 3.0f, 4.5f };
float fun(float f)
{
return f;
}
// Regression: int value must be promoted to float
float ret_int()
{
return 5;
}
int main()
{
// Int/float casts
float x = 4.0f;
float y = 5;
assert((int)x == 4);
assert((int)3.0f == 3);
assert((int)(float)3 == 3);
assert((int)-5000.0f == -5000);
assert((short)5000.0f == 5000);
assert((short)-5000.0f == (short)-5000);
assert((float)(short)5000 == 5000.0f);
assert((float)(unsigned int)700 == 700.0f);
// Global variable access
assert(g == 3.5f);
g = 4.0f;
assert(g == 4.0f);
assert(f_array[0] == 1.0f);
// Automatic promotion of int argument to float
assert(fun(4) == 4.0f);
// Automatic promotion of return value to float
assert(ret_int() == 5.0f);
// Floating-point comparisons
assert(0.0f == 0.0f);
assert(0.0f != 1.0f);
assert(0.0f < 1.0f);
assert(-1.0f < 0.0f);
// Ternary expression with mismatched types
assert((0? 1.5f:2) == 2.0f);
// Arithmetic
assert(1.0f + 2.0f == 3.0f);
assert(2.0f * 3.0f == 6.0f);
assert(6.0f / 2.0f == 3.0f);
assert(powf(0.0f, 1.0f) == 0.0f);
assert(powf(1.0f, 2.0f) == 1.0f);
assert(powf(2.0f, 3.0f) == 8.0f);
assert(sqrtf(0.0f) == 0.0f);
assert(sqrtf(4.0f) == 2.0f);
assert(sinf(0.0f) == 0.0f);
assert(floorf(4.5f) == 4.0f);
assert(floorf(-4.5f) == -5.0f);
assert(truncf(0.0f) == 0.0f);
assert(truncf(4.5f) == 4.0f);
assert(truncf(-4.5f) == -4.0f);
return 0;
}