float: Add class instances for Float.
[altfloat.git] / cfloat.c
blobd1f58f22d5929914ab0fd4af62f3006c27f25696
1 /*
2 * Floating point functions that are difficult or impossible to implement
3 * in pure Haskell.
5 * Copyright (C) 2009 Nick Bowler.
7 * License BSD2: 2-clause BSD license. See LICENSE for full terms.
8 * This is free software: you are free to change and redistribute it.
9 * There is NO WARRANTY, to the extent permitted by law.
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <errno.h>
14 #include <math.h>
16 #include "cfloat.h"
18 int double_format(char *buf, const char *fmt, double val)
20 if (buf == NULL)
21 return snprintf(NULL, 0, fmt, val);
22 return sprintf(buf, fmt, val);
25 double double_signum(double val)
27 if (signbit(val))
28 return -1;
29 return 1;
32 float float_signum(float val)
34 if (signbit(val))
35 return -1;
36 return 1;
39 int double_classify(double val)
41 switch (fpclassify(val)) {
42 case FP_INFINITE:
43 return 0;
44 case FP_NAN:
45 return 1;
46 case FP_NORMAL:
47 return 2;
48 case FP_SUBNORMAL:
49 return 3;
50 case FP_ZERO:
51 return 4;
54 return -1;
57 int float_classify(float val)
59 switch (fpclassify(val)) {
60 case FP_INFINITE:
61 return 0;
62 case FP_NAN:
63 return 1;
64 case FP_NORMAL:
65 return 2;
66 case FP_SUBNORMAL:
67 return 3;
68 case FP_ZERO:
69 return 4;
72 return -1;
75 int double_compare(double a, double b)
77 if (isless(a, b))
78 return 0;
79 if (a == b)
80 return 1;
81 if (isgreater(a, b))
82 return 2;
83 if (isunordered(a, b))
84 return 3;
85 return -1;
88 int float_compare(float a, float b)
90 if (isless(a, b))
91 return 0;
92 if (a == b)
93 return 1;
94 if (isgreater(a, b))
95 return 2;
96 if (isunordered(a, b))
97 return 3;
98 return -1;