Implement J3ML Core Math & Unit Tests
This commit is contained in:
@@ -17,6 +17,14 @@
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
|
||||
/// TODO: Implement lookup tables.
|
||||
|
||||
#ifdef USE_LOOKUP_TABLES
|
||||
|
||||
static float fast_cossin_table[MAX_CIRCLE_ANGLE];
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#include <J3ML/Algorithm/Reinterpret.hpp>
|
||||
|
||||
@@ -199,8 +207,12 @@ namespace J3ML::Math::Functions {
|
||||
/// Computes the relative error of the two variables.
|
||||
float RelativeError(float a, float b);
|
||||
|
||||
inline bool IsFinite(float f) { return (ReinterpretAs<u32>(f) << 1) < 0xFF000000u; }
|
||||
inline bool IsFinite(double d) { return (ReinterpretAs<u64>(d) << 1) < 0xFFE0000000000000ULL; }
|
||||
template <typename T> bool IsFinite(T) { return true;}
|
||||
|
||||
template<> inline bool IsFinite(float f) { return (ReinterpretAs<u32>(f) << 1) < 0xFF000000u; }
|
||||
template<> inline bool IsFinite(double d) { return (ReinterpretAs<u64>(d) << 1) < 0xFFE0000000000000ULL; }
|
||||
template<> inline bool IsFinite(u32 i) { return (i << 1) < 0xFF000000u; }
|
||||
template<> inline bool IsFinite(u64 i) { return (i << 1) < 0xFFE0000000000000ULL;}
|
||||
|
||||
inline bool IsNotANumber(float f) { return (ReinterpretAs<u32>(f) << 1) > 0xFF000000u; }
|
||||
inline bool IsNotANumber(double d) { return (ReinterpretAs<u64>(d) << 1) > 0xFFE0000000000000ULL; }
|
||||
|
8
include/J3ML/Math.hpp
Normal file
8
include/J3ML/Math.hpp
Normal file
@@ -0,0 +1,8 @@
|
||||
//
|
||||
// Created by dawsh on 7/6/24.
|
||||
//
|
||||
|
||||
#ifndef MATH_HPP
|
||||
#define MATH_HPP
|
||||
|
||||
#endif //MATH_HPP
|
83
tests/MathFuncTests.hpp
Normal file
83
tests/MathFuncTests.hpp
Normal file
@@ -0,0 +1,83 @@
|
||||
/// Josh's 3D Math Library
|
||||
/// A C++20 Library for 3D Math, Computer Graphics, and Scientific Computing.
|
||||
/// Developed and Maintained by Josh O'Leary @ Redacted Software.
|
||||
/// Special Thanks to William Tomasine II and Maxine Hayes.
|
||||
/// (c) 2024 Redacted Software
|
||||
/// This work is dedicated to the public domain.
|
||||
|
||||
/// @file MathFuncTests.hpp
|
||||
/// @desc Unit tests for core math functions
|
||||
/// @edit 2024-07-06
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <jtest/jtest.hpp>
|
||||
#include <J3ML/J3ML.h>
|
||||
|
||||
inline void MathFuncTests()
|
||||
{
|
||||
using namespace jtest;
|
||||
using namespace J3ML::Math;
|
||||
|
||||
TEST("Math::IsFinite", [] {
|
||||
check(IsFinite<u32>(420));
|
||||
check(IsFinite<u64>(25));
|
||||
check(IsFinite(5.f));
|
||||
check(IsFinite(5.0));
|
||||
check(!IsFinite(Infinity));
|
||||
check(!IsFinite(NotANumber));
|
||||
});
|
||||
|
||||
TEST("Math::IsNotANumber", [] {
|
||||
check(!IsNotANumber(5.f));
|
||||
check(!IsNotANumber(5.0));
|
||||
check(IsNotANumber(NotANumber));
|
||||
check(!IsNotANumber(Infinity));
|
||||
});
|
||||
|
||||
TEST("Math::IsInfinite", [] {
|
||||
check(!IsInfinite(5.f));
|
||||
check(!IsInfinite(5.0));
|
||||
check(IsInfinite(Infinity));
|
||||
});
|
||||
|
||||
TEST("Math::ReinterpretAsU32", [] {
|
||||
check(ReinterpretAs<u32>(0.f) == 0x00000000);
|
||||
check(ReinterpretAs<u32>(1.f) == 0x3F800000);
|
||||
check(ReinterpretAs<u32>(2.f) == 0x40000000);
|
||||
check(ReinterpretAs<u32>(-1.f) == 0xBF800000);
|
||||
check(ReinterpretAs<u32>(Infinity) == 0x7F800000);
|
||||
});
|
||||
|
||||
TEST("Math::ReinterpretAsFloat", [] {
|
||||
check(ReinterpretAs<float, u32>(0x00000000) == 0.f);
|
||||
check(ReinterpretAs<float, u32>(0x3F800000) == 1.f);
|
||||
check(ReinterpretAs<float, u32>(0x40000000) == 2.f);
|
||||
check(ReinterpretAs<float, u32>(0xBF800000) == -1.f);
|
||||
check(ReinterpretAs<float>(0x7F800000) == Infinity);
|
||||
check(IsNotANumber(ReinterpretAs<float>(0x7F800001)));
|
||||
});
|
||||
|
||||
TEST("Math::SqrtVals", [] {
|
||||
|
||||
});
|
||||
|
||||
TEST("Math::SqrtPrecision", [] {
|
||||
|
||||
});
|
||||
|
||||
TEST("Math::SqrtRSqrtPrecision", [] {
|
||||
|
||||
});
|
||||
|
||||
TEST("Math::SqrtRecipPrecision", [] {
|
||||
|
||||
});
|
||||
|
||||
TEST("Math::Min", [] {});
|
||||
TEST("Math::Max", [] {});
|
||||
TEST("Math::IsPow2", [] {});
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user