// filename:c2011-F-10-7-1-ex.c
// original examples and/or notes:
// 		(c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
// 			C2011 F.10.7.1 The fmod functions
// compile and output mechanism:
// 		(c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, January.04, 2014
// compile errors and/or wornings:
// 1	(c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
// 			Target: x86_64-apple-darwin11.4.2 //Thread model: posix
// 		(c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.
// 2    gcc-4.9 (GCC) 4.9.0 20131229 (experimental)
//      Copyright (C) 2013 Free Software Foundation, Inc.
#include <stdio.h>
#include <math.h>
#include <fenv.h>
#pragma STDC FENV_ACCESS ON
double fmod_a(double x, double y)
{
double result;
result = remainder(fabs(x), (y = fabs(y)));
if (signbit(result)) result += y;
return copysign(result, x);
}
#define MAX 5
int main(){
	double value[]={3.14159,-2.5, 0.0, FP_INFINITE,FP_NAN},value2[]={3.14159,-2.5, 0.0, FP_INFINITE,FP_NAN},ret,ret_a;
	for(int i=0;i<MAX;i++){
			for(int j=0;j<MAX;j++){
		ret = fmod(value[i],value2[j]);
		ret_a = fmod_a(value[i],value2[j]);
			printf("fmod( %f,%f) = %f, fmod_a() = %f, true=%d\n", value[i],value2[j], ret, ret_a,(ret==ret_a) );
		}
	}
	return printf("F.10.7.1 The fmod functions\n");
}
// 1. warning LLVM 3.2
//c2011-F-10-7-1-ex.c:17:14: warning: pragma STDC FENV_ACCESS ON is not supported, ignoring pragma [-Wunknown-pragmas]
//#pragma STDC FENV_ACCESS ON
//             ^
//1 warning generated.
//  output LLVM 3.2 & GCC4.9 
// fmod( 3.141590,3.141590) = 0.000000, fmod_a() = 0.000000, true=1
// fmod( 3.141590,-2.500000) = 0.641590, fmod_a() = 0.641590, true=1
// fmod( 3.141590,0.000000) = nan, fmod_a() = nan, true=0
// fmod( 3.141590,2.000000) = 1.141590, fmod_a() = 1.141590, true=1
// fmod( 3.141590,1.000000) = 0.141590, fmod_a() = 0.141590, true=1
// fmod( -2.500000,3.141590) = -2.500000, fmod_a() = -2.500000, true=1
// fmod( -2.500000,-2.500000) = -0.000000, fmod_a() = -0.000000, true=1
// fmod( -2.500000,0.000000) = nan, fmod_a() = nan, true=0
// fmod( -2.500000,2.000000) = -0.500000, fmod_a() = -0.500000, true=1
// fmod( -2.500000,1.000000) = -0.500000, fmod_a() = -0.500000, true=1
// fmod( 0.000000,3.141590) = 0.000000, fmod_a() = 0.000000, true=1
// fmod( 0.000000,-2.500000) = 0.000000, fmod_a() = 0.000000, true=1
// fmod( 0.000000,0.000000) = nan, fmod_a() = nan, true=0
// fmod( 0.000000,2.000000) = 0.000000, fmod_a() = 0.000000, true=1
// fmod( 0.000000,1.000000) = 0.000000, fmod_a() = 0.000000, true=1
// fmod( 2.000000,3.141590) = 2.000000, fmod_a() = 2.000000, true=1
// fmod( 2.000000,-2.500000) = 2.000000, fmod_a() = 2.000000, true=1
// fmod( 2.000000,0.000000) = nan, fmod_a() = nan, true=0
// fmod( 2.000000,2.000000) = 0.000000, fmod_a() = 0.000000, true=1
// fmod( 2.000000,1.000000) = 0.000000, fmod_a() = 0.000000, true=1
// fmod( 1.000000,3.141590) = 1.000000, fmod_a() = 1.000000, true=1
// fmod( 1.000000,-2.500000) = 1.000000, fmod_a() = 1.000000, true=1
// fmod( 1.000000,0.000000) = nan, fmod_a() = nan, true=0
// fmod( 1.000000,2.000000) = 1.000000, fmod_a() = 1.000000, true=1
// fmod( 1.000000,1.000000) = 0.000000, fmod_a() = 0.000000, true=1
// F.10.7.1 The fmod functions