1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-07-05 10:28:58 +00:00

Introduced a couple more floating-point conversion tests, fixed errors uncovered.

This commit is contained in:
Thomas Harte 2016-12-24 23:21:19 -05:00
parent e25195a718
commit 09ff9d6a26
2 changed files with 35 additions and 3 deletions

View File

@ -15,10 +15,23 @@
@implementation TimeTests
- (void)testFloat
- (void)testHalf
{
Storage::Time half(0.5f);
XCTAssert(half == Storage::Time(1, 2), @"0.5 should be converted to 1/2");
}
- (void)testTwenty
{
Storage::Time twenty(20.0f);
XCTAssert(twenty == Storage::Time(20, 1), @"20.0 should be converted to 20/1");
}
- (void)testTooSmallFloat
{
float original = 1.0f / powf(2.0f, 25.0f);
Storage::Time time(original);
XCTAssert(time == Storage::Time(0), @"Numbers too small to be represented should be 0");
}
@end

View File

@ -223,8 +223,27 @@ struct Time {
float mantissa = frexpf(value, &exponent);
float loaded_mantissa = ldexpf(mantissa, 24);
uint64_t result_length = (uint64_t)loaded_mantissa;
uint64_t result_clock_rate = 1 << (exponent + 24);
uint64_t result_length;
uint64_t result_clock_rate;
if(exponent < 0)
{
int right_shift = -exponent;
result_length = (uint64_t)loaded_mantissa >> right_shift;
result_clock_rate = 1;
}
else
{
if(exponent <= 24)
{
result_length = (uint64_t)loaded_mantissa;
result_clock_rate = 1 << (24 - exponent);
}
else
{
result_length = std::numeric_limits<uint64_t>::max();
result_clock_rate = 1;
}
}
install_result(result_length, result_clock_rate);
}
};