Changeset 1003
- Timestamp:
- Oct 3, 2011, 1:36:31 AM (11 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/real.cpp
r1002 r1003 684 684 else 685 685 return ret + real::R_1; 686 } 687 688 real round(real const &x) 689 { 690 if (x < real::R_0) 691 return -round(-x); 692 693 return floor(x + (real::R_1 >> 1)); 694 } 695 696 real fmod(real const &x, real const &y) 697 { 698 if (!y) 699 return real::R_0; /* FIXME: return NaN */ 700 701 if (!x) 702 return x; 703 704 real tmp = round(x / y); 705 return x - tmp * y; 686 706 } 687 707 -
trunk/src/real.h
r1002 r1003 72 72 friend real floor(real const &x); 73 73 friend real ceil(real const &x); 74 friend real round(real const &x); 75 friend real fmod(real const &x, real const &y); 74 76 75 77 friend real sin(real const &x); -
trunk/test/unit/real.cpp
r1002 r1003 258 258 } 259 259 260 LOLUNIT_TEST(FloorCeil )260 LOLUNIT_TEST(FloorCeilEtc) 261 261 { 262 262 double tests[] = 263 263 { 264 -2.0, -2.0, -2.0, 265 -1.5, -2.0, -1.0, 266 -1.0, -1.0, -1.0, 267 -0.0, -0.0, -0.0, 268 0.0, 0.0, 0.0, 269 0.25, 0.0, 1.0, 270 0.375, 0.0, 1.0, 271 0.5, 0.0, 1.0, 272 1.0, 1.0, 1.0, 273 1.5, 1.0, 2.0, 274 2.0, 2.0, 2.0, 275 2.5, 2.0, 3.0, 276 3.0, 3.0, 3.0, 277 8192.0, 8192.0, 8192.0, 278 8192.03125, 8192.0, 8193.0, 279 8192.5, 8192.0, 8193.0, 280 8193.0, 8193.0, 8193.0, 281 549755813888.0, 549755813888.0, 549755813888.0, 282 549755813888.03125, 549755813888.0, 549755813889.0, 283 549755813888.5, 549755813888.0, 549755813889.0, 284 549755813889.0, 549755813889.0, 549755813889.0, 264 -2.0, -2.0, -2.0, -2.0, 265 -1.5, -2.0, -1.0, -2.0, 266 -1.0, -1.0, -1.0, -1.0, 267 -0.0, -0.0, -0.0, -0.0, 268 0.0, 0.0, 0.0, 0.0, 269 0.25, 0.0, 1.0, 0.0, 270 0.375, 0.0, 1.0, 0.0, 271 0.5, 0.0, 1.0, 1.0, 272 1.0, 1.0, 1.0, 1.0, 273 1.5, 1.0, 2.0, 2.0, 274 2.0, 2.0, 2.0, 2.0, 275 2.5, 2.0, 3.0, 3.0, 276 3.0, 3.0, 3.0, 3.0, 277 8192.0, 8192.0, 8192.0, 8192.0, 278 8192.03125, 8192.0, 8193.0, 8192.0, 279 8192.5, 8192.0, 8193.0, 8193.0, 280 8193.0, 8193.0, 8193.0, 8193.0, 281 549755813888.0, 549755813888.0, 549755813888.0, 549755813888.0, 282 549755813888.03125, 549755813888.0, 549755813889.0, 549755813888.0, 283 549755813888.5, 549755813888.0, 549755813889.0, 549755813889.0, 284 549755813889.0, 549755813889.0, 549755813889.0, 549755813889.0, 285 285 }; 286 286 287 for (unsigned int n = 0; n < sizeof(tests) / sizeof(*tests); n += 3)287 for (unsigned int n = 0; n < sizeof(tests) / sizeof(*tests); n += 4) 288 288 { 289 289 double a0 = floor((real)tests[n]); … … 291 291 double a1 = ceil((real)tests[n]); 292 292 double b1 = tests[n + 2]; 293 double a2 = round((real)tests[n]); 294 double b2 = tests[n + 3]; 293 295 294 296 LOLUNIT_ASSERT_EQUAL(b0, a0); 295 297 LOLUNIT_ASSERT_EQUAL(b1, a1); 298 LOLUNIT_ASSERT_EQUAL(b2, a2); 296 299 } 297 300 }
Note: See TracChangeset
for help on using the changeset viewer.