<commit_before>/* 
 * TTUnitTest
 * Copyright © 2011, <NAME> and <NAME>
 * 
 * License: This code is licensed under the terms of the "New BSD License"
 * http://creativecommons.org/licenses/BSD/
 */

#include "TTUnitTest.h"


static const TTFloat32 kTTTestFloat32Epsilon = 0.00001;
static const TTFloat64 kTTTestFloat64Epsilon = 0.000000001;


TTBoolean TTTestFloatEquivalence(TTFloat32 a, TTFloat32 b)
{
	if (fabs(a-b) <= fabs(a*kTTTestFloat32Epsilon))
		return true;
	else
		return false;
}


TTBoolean TTTestFloatEquivalence(TTFloat64 a, TTFloat64 b)
{
	if (a != TTAntiDenormal(a))
		TTTestLog("WARNING: TESTING FLOAT EQUIVALENCE ON A DENORMAL!");

	// TODO: Denormal checking on b
	// TODO: checking for NaN
	// TODO: checking for INF
	
	// TODO: research re-writing these functions according to http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
	
#ifdef THE_WAY_WE_DID_IT_IN_BERGEN
	if (fabs(a-b) <= fabs(TTAntiDenormal(a*kTTTestFloat64Epsilon)))
		return true;
	else
		return false;
#else
	if (fabs(a-b) < kTTTestFloat64Epsilon)
		return true;
	else
		return false;
#endif
}


void TTTestLog(const char *fmtstring, ...)
{
	char	str[4096];
	char	fullstr[4096];<commit_msg>More solid method for comparing floats<commit_after>/* 
 * TTUnitTest
 * Copyright © 2011, <NAME> and <NAME>
 * 
 * License: This code is licensed under the terms of the "New BSD License"
 * http://creativecommons.org/licenses/BSD/
 */

#include "TTUnitTest.h"


static const TTFloat32 kTTTestFloat32Epsilon = 0.00001;
static const TTFloat64 kTTTestFloat64Epsilon = 0.000000001;


TTBoolean TTTestFloatEquivalence(TTFloat32 a, TTFloat32 b)
{
	// Following method is based on 
	// http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
	
	// TODO: Make maxUlps a constant or argument to the method?
	// Make sure maxUlps is non-negative and small enough that the
	// default NAN won't compare as equal to anything.
	TTInt64 maxUlps = 5;
	// assert(maxUlps > 0 && maxUlps < 4 * 1024 * 1024);
	
	TTInt32 aInt = *(TTInt32*)&a;
	
	// Make aInt lexicographically ordered as a twos-complement int
	if (aInt < 0)
		aInt = 0x80000000 - aInt;
	// Make bInt lexicographically ordered as a twos-complement int
	TTInt32 bInt = *(TTInt32*)&b;
	
	if (bInt < 0)
		bInt = 0x80000000 - bInt;
	TTInt32 intDiff = abs(aInt - bInt);
	if (intDiff <= maxUlps)
		return true;
	return false;
	
}


TTBoolean TTTestFloatEquivalence(TTFloat64 a, TTFloat64 b)
{
	if (a != TTAntiDenormal(a))
		TTTestLog("WARNING: TESTING FLOAT EQUIVALENCE ON A DENORMAL!");

	// TODO: Denormal checking on b
	// TODO: checking for NaN
	// TODO: checking for INF
	
	// Following method is based on 
	// http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

	// Make sure maxUlps is non-negative and small enough that the
	// default NAN won't compare as equal to anything.
	// TODO: Make maxUlps a constant or argument to the method?
	TTInt64 maxUlps = 5;
	// assert(maxUlps > 0 && maxUlps < 4 * 1024 * 1024);
	
	TTInt64 aInt = *(TTInt64*)&a;
	
	// Make aInt lexicographically ordered as a twos-complement int
	if (aInt < 0)
		aInt = 0x80000000 - aInt;
	// Make bInt lexicographically ordered as a twos-complement int
	TTInt64 bInt = *(TTInt64*)&b;
	
	if (bInt < 0)
		bInt = 0x80000000 - bInt;
	TTInt64 intDiff = abs(aInt - bInt);
	if (intDiff <= maxUlps)
		return true;
	return false;
}


void TTTestLog(const char *fmtstring, ...)
{
	char	str[4096];
	char	fullstr[4096];
{'fraction_non_alphanumeric': 0.10992498557414887, 'fraction_numerical': 0.05193306405077899, 'mean_word_length': 3.848951048951049, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3840969', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1246, 'n_words': 366}