packages feed

altfloat-0.2: cfloat.c

/*
 * Floating point functions that are difficult or impossible to implement
 * in pure Haskell.
 *
 * Copyright (C) 2009 Nick Bowler.
 *
 * License BSD2:  2-clause BSD license.  See LICENSE for full terms.
 * This is free software: you are free to change and redistribute it.
 * There is NO WARRANTY, to the extent permitted by law.
 */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>

#include "cfloat.h"

int double_format(char *buf, const char *fmt, double val)
{
	if (buf == NULL)
		return snprintf(NULL, 0, fmt, val);
	return sprintf(buf, fmt, val);
}

double double_signum(double val)
{
	if (signbit(val))
		return -1;
	return 1;
}

int double_classify(double val)
{
	switch (fpclassify(val)) {
	case FP_INFINITE:
		return 0;
	case FP_NAN:
		return 1;
	case FP_NORMAL:
		return 2;
	case FP_SUBNORMAL:
		return 3;
	case FP_ZERO:
		return 4;
	}

	return -1;
}

int double_compare(double a, double b)
{
	if (isless(a, b))
		return 0;
	if (a == b)
		return 1;
	if (isgreater(a, b))
		return 2;
	if (isunordered(a, b))
		return 3;
	return -1;
}