From 09ff9d6a261b7531cea071d5c4c28737474413fc Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sat, 24 Dec 2016 23:21:19 -0500 Subject: [PATCH] Introduced a couple more floating-point conversion tests, fixed errors uncovered. --- OSBindings/Mac/Clock SignalTests/TimeTests.mm | 15 +++++++++++- Storage/Storage.hpp | 23 +++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/OSBindings/Mac/Clock SignalTests/TimeTests.mm b/OSBindings/Mac/Clock SignalTests/TimeTests.mm index 839c8f1b0..fc0e0615b 100644 --- a/OSBindings/Mac/Clock SignalTests/TimeTests.mm +++ b/OSBindings/Mac/Clock SignalTests/TimeTests.mm @@ -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 diff --git a/Storage/Storage.hpp b/Storage/Storage.hpp index f93ba674e..7220f8450 100644 --- a/Storage/Storage.hpp +++ b/Storage/Storage.hpp @@ -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::max(); + result_clock_rate = 1; + } + } install_result(result_length, result_clock_rate); } };