altfloat-0.2.2
[altfloat.git] / cfloat.c
blob7b927584f9d3ca636c86b8199710617c4c7175da
1 /*
2 * Floating point functions that are difficult or impossible to implement
3 * in pure Haskell.
5 * Copyright (C) 2009-2010 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 int double_classify(double val)
34 switch (fpclassify(val)) {
35 case FP_INFINITE:
36 return 0;
37 case FP_NAN:
38 return 1;
39 case FP_NORMAL:
40 return 2;
41 case FP_SUBNORMAL:
42 return 3;
43 case FP_ZERO:
44 return 4;
47 return -1;
50 int double_compare(double a, double b)
52 if (isless(a, b))
53 return 0;
54 if (a == b)
55 return 1;
56 if (isgreater(a, b))
57 return 2;
58 if (isunordered(a, b))
59 return 3;
60 return -1;