mirror of
https://github.com/TomHarte/CLK.git
synced 2025-01-11 08:30:55 +00:00
Introduced a couple more floating-point conversion tests, fixed errors uncovered.
This commit is contained in:
parent
e25195a718
commit
09ff9d6a26
@ -15,10 +15,23 @@
|
|||||||
|
|
||||||
@implementation TimeTests
|
@implementation TimeTests
|
||||||
|
|
||||||
- (void)testFloat
|
- (void)testHalf
|
||||||
{
|
{
|
||||||
Storage::Time half(0.5f);
|
Storage::Time half(0.5f);
|
||||||
XCTAssert(half == Storage::Time(1, 2), @"0.5 should be converted to 1/2");
|
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
|
@end
|
||||||
|
@ -223,8 +223,27 @@ struct Time {
|
|||||||
float mantissa = frexpf(value, &exponent);
|
float mantissa = frexpf(value, &exponent);
|
||||||
float loaded_mantissa = ldexpf(mantissa, 24);
|
float loaded_mantissa = ldexpf(mantissa, 24);
|
||||||
|
|
||||||
uint64_t result_length = (uint64_t)loaded_mantissa;
|
uint64_t result_length;
|
||||||
uint64_t result_clock_rate = 1 << (exponent + 24);
|
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);
|
install_result(result_length, result_clock_rate);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user