about summary refs log tree commit diff stats
path: root/021arithmetic.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-05-12 18:08:47 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-05-12 18:08:47 -0700
commitfca0ebbe0cc01d37e47822d8a62ea062a845f33d (patch)
tree557ef7d0bd5d3ddc98ab47cf0273c99cc4a09630 /021arithmetic.cc
parent98f3a94201df11501d417ac2e75a756ab54ac873 (diff)
downloadmu-fca0ebbe0cc01d37e47822d8a62ea062a845f33d.tar.gz
1360 - store doubles in memory
This is a far cleaner way to provide *some* floating-point support. We
can only represent signed integers up to 2^51 rather than 2^63. But in
exchange we don't have to worry about it elsewhere, and it's probably
faster than checking tag bits in every operation.

Hmm, yeah, surprised how easy this was. I think I'll give up on the
other approach.

I still don't have non-integer literals. But we won't bother with those
until we need them. `3.14159:literal` seems ugly.
Diffstat (limited to '021arithmetic.cc')
-rw-r--r--021arithmetic.cc17
1 files changed, 12 insertions, 5 deletions
diff --git a/021arithmetic.cc b/021arithmetic.cc
index 307bcb18..0dedd02d 100644
--- a/021arithmetic.cc
+++ b/021arithmetic.cc
@@ -6,7 +6,7 @@ ADD,
 Recipe_number["add"] = ADD;
 :(before "End Primitive Recipe Implementations")
 case ADD: {
-  long long int result = 0;
+  double result = 0;
   for (index_t i = 0; i < ingredients.size(); ++i) {
     assert(ingredients.at(i).size() == 1);  // scalar
     result += ingredients.at(i).at(0);
@@ -53,7 +53,7 @@ Recipe_number["subtract"] = SUBTRACT;
 :(before "End Primitive Recipe Implementations")
 case SUBTRACT: {
   assert(ingredients.at(0).size() == 1);  // scalar
-  long long int result = ingredients.at(0).at(0);
+  double result = ingredients.at(0).at(0);
   for (index_t i = 1; i < ingredients.size(); ++i) {
     assert(ingredients.at(i).size() == 1);  // scalar
     result -= ingredients.at(i).at(0);
@@ -99,7 +99,7 @@ MULTIPLY,
 Recipe_number["multiply"] = MULTIPLY;
 :(before "End Primitive Recipe Implementations")
 case MULTIPLY: {
-  long long int result = 1;
+  double result = 1;
   for (index_t i = 0; i < ingredients.size(); ++i) {
     assert(ingredients.at(i).size() == 1);  // scalar
     result *= ingredients.at(i).at(0);
@@ -146,7 +146,7 @@ Recipe_number["divide"] = DIVIDE;
 :(before "End Primitive Recipe Implementations")
 case DIVIDE: {
   assert(ingredients.at(0).size() == 1);  // scalar
-  long long int result = ingredients.at(0).at(0);
+  double result = ingredients.at(0).at(0);
   for (index_t i = 1; i < ingredients.size(); ++i) {
     assert(ingredients.at(i).size() == 1);  // scalar
     result /= ingredients.at(i).at(0);
@@ -193,7 +193,7 @@ Recipe_number["divide-with-remainder"] = DIVIDE_WITH_REMAINDER;
 :(before "End Primitive Recipe Implementations")
 case DIVIDE_WITH_REMAINDER: {
   long long int quotient = ingredients.at(0).at(0) / ingredients.at(1).at(0);
-  long long int remainder = ingredients.at(0).at(0) % ingredients.at(1).at(0);
+  long long int remainder = static_cast<long long int>(ingredients.at(0).at(0)) % static_cast<long long int>(ingredients.at(1).at(0));
   products.resize(2);
   products.at(0).push_back(quotient);
   products.at(1).push_back(remainder);
@@ -227,3 +227,10 @@ recipe main [
 +mem: storing 2 in location 3
 +run: product 1 is 4
 +mem: storing 5 in location 4
+
+:(scenario divide_with_decimal_point)
+recipe main [
+  # todo: literal floats?
+  1:integer <- divide 5:literal, 2:literal
+]
++mem: storing 2.5 in location 1
> 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351