diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2016-10-31 22:56:25 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2016-10-31 22:56:25 -0700 |
commit | 9d9da2adf96d783ae51edc1bf6c9cbe6017ead8f (patch) | |
tree | 8e34d2dbc40041a06cc44110a3973aed90f40e55 | |
parent | 1167f8779c2de268a074fc8656f022721df56c3f (diff) | |
download | mu-9d9da2adf96d783ae51edc1bf6c9cbe6017ead8f.tar.gz |
3618
-rw-r--r-- | 022arithmetic.cc | 14 | ||||
-rw-r--r-- | 070table.mu | 4 | ||||
-rw-r--r-- | html/022arithmetic.cc.html | 14 | ||||
-rw-r--r-- | html/070table.mu.html | 4 |
4 files changed, 18 insertions, 18 deletions
diff --git a/022arithmetic.cc b/022arithmetic.cc index f46666f1..23f9a39d 100644 --- a/022arithmetic.cc +++ b/022arithmetic.cc @@ -269,8 +269,9 @@ case DIVIDE_WITH_REMAINDER: { :(before "End Primitive Recipe Implementations") case DIVIDE_WITH_REMAINDER: { products.resize(2); - int a = static_cast<int>(ingredients.at(0).at(0)); - int b = static_cast<int>(ingredients.at(1).at(0)); + // fractions will be dropped; very large numbers will overflow + long long int a = static_cast<long long int>(ingredients.at(0).at(0)); + long long int b = static_cast<long long int>(ingredients.at(1).at(0)); if (b == 0) { raise << maybe(current_recipe_name()) << "divide by zero in '" << to_original_string(current_instruction()) << "'\n" << end(); products.resize(2); @@ -278,11 +279,10 @@ case DIVIDE_WITH_REMAINDER: { products.at(1).push_back(0); break; } - int quotient = a / b; - int remainder = a % b; - // very large integers will lose precision - products.at(0).push_back(quotient); - products.at(1).push_back(remainder); + long long int quotient = a / b; + long long int remainder = a % b; + products.at(0).push_back(static_cast<double>(quotient)); + products.at(1).push_back(static_cast<double>(remainder)); break; } diff --git a/070table.mu b/070table.mu index d567475c..171bb2a8 100644 --- a/070table.mu +++ b/070table.mu @@ -52,7 +52,7 @@ def put-index table:&:table:_key:_value, key:_key, value:_value -> table:&:table hash <- abs hash capacity:num <- get *table, capacity:offset _, hash-key:num <- divide-with-remainder hash, capacity - hash-key <- abs hash-key # in case hash-key overflows into a negative integer + hash-key <- abs hash-key # in case hash overflows from a double into a negative integer inside 'divide-with-remainder' above table-data:&:@:table-row:_key:_value <- get *table, data:offset x:table-row:_key:_value <- index *table-data, hash-key occupied?:bool <- get x, occupied?:offset @@ -69,7 +69,7 @@ def index table:&:table:_key:_value, key:_key -> result:_value [ hash <- abs hash capacity:num <- get *table, capacity:offset _, hash-key:num <- divide-with-remainder hash, capacity - hash-key <- abs hash-key # in case hash-key overflows into a negative integer + hash-key <- abs hash-key # in case hash overflows from a double into a negative integer inside 'divide-with-remainder' above table-data:&:@:table-row:_key:_value <- get *table, data:offset x:table-row:_key:_value <- index *table-data, hash-key occupied?:bool <- get x, occupied?:offset diff --git a/html/022arithmetic.cc.html b/html/022arithmetic.cc.html index 7a0e24b9..4d2b454e 100644 --- a/html/022arithmetic.cc.html +++ b/html/022arithmetic.cc.html @@ -305,8 +305,9 @@ put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span <span class="Delimiter">:(before "End Primitive Recipe Implementations")</span> <span class="Normal">case</span> DIVIDE_WITH_REMAINDER: <span class="Delimiter">{</span> products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">2</span><span class="Delimiter">);</span> - <span class="Normal">int</span> a = <span class="Normal">static_cast</span><<span class="Normal">int</span>><span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">));</span> - <span class="Normal">int</span> b = <span class="Normal">static_cast</span><<span class="Normal">int</span>><span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">));</span> + <span class="Comment">// fractions will be dropped; very large numbers will overflow</span> + <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> a = <span class="Normal">static_cast</span><<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>><span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">));</span> + <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> b = <span class="Normal">static_cast</span><<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>><span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">));</span> <span class="Normal">if</span> <span class="Delimiter">(</span>b == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> raise << maybe<span class="Delimiter">(</span>current_recipe_name<span class="Delimiter">())</span> << <span class="Constant">"divide by zero in '"</span> << to_original_string<span class="Delimiter">(</span>current_instruction<span class="Delimiter">())</span> << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span> products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">2</span><span class="Delimiter">);</span> @@ -314,11 +315,10 @@ put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span> <span class="Identifier">break</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> - <span class="Normal">int</span> quotient = a / b<span class="Delimiter">;</span> - <span class="Normal">int</span> remainder = a % b<span class="Delimiter">;</span> - <span class="Comment">// very large integers will lose precision</span> - products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>quotient<span class="Delimiter">);</span> - products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>remainder<span class="Delimiter">);</span> + <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> quotient = a / b<span class="Delimiter">;</span> + <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> remainder = a % b<span class="Delimiter">;</span> + products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span><span class="Normal">static_cast</span><<span class="Normal">double</span>><span class="Delimiter">(</span>quotient<span class="Delimiter">));</span> + products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span><span class="Normal">static_cast</span><<span class="Normal">double</span>><span class="Delimiter">(</span>remainder<span class="Delimiter">));</span> <span class="Identifier">break</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> diff --git a/html/070table.mu.html b/html/070table.mu.html index 7d46ec53..b843f50f 100644 --- a/html/070table.mu.html +++ b/html/070table.mu.html @@ -86,7 +86,7 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color hash <span class="Special"><-</span> abs hash capacity:num <span class="Special"><-</span> get *table, <span class="Constant">capacity:offset</span> _, hash-key:num <span class="Special"><-</span> divide-with-remainder hash, capacity - hash-key <span class="Special"><-</span> abs hash-key <span class="Comment"># in case hash-key overflows into a negative integer</span> + hash-key <span class="Special"><-</span> abs hash-key <span class="Comment"># in case hash overflows from a double into a negative integer inside 'divide-with-remainder' above</span> table-data:&:@:table-row:_key:_value <span class="Special"><-</span> get *table, <span class="Constant">data:offset</span> x:table-row:_key:_value <span class="Special"><-</span> index *table-data, hash-key occupied?:bool <span class="Special"><-</span> get x, <span class="Constant">occupied?:offset</span> @@ -103,7 +103,7 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color hash <span class="Special"><-</span> abs hash capacity:num <span class="Special"><-</span> get *table, <span class="Constant">capacity:offset</span> _, hash-key:num <span class="Special"><-</span> divide-with-remainder hash, capacity - hash-key <span class="Special"><-</span> abs hash-key <span class="Comment"># in case hash-key overflows into a negative integer</span> + hash-key <span class="Special"><-</span> abs hash-key <span class="Comment"># in case hash overflows from a double into a negative integer inside 'divide-with-remainder' above</span> table-data:&:@:table-row:_key:_value <span class="Special"><-</span> get *table, <span class="Constant">data:offset</span> x:table-row:_key:_value <span class="Special"><-</span> index *table-data, hash-key occupied?:bool <span class="Special"><-</span> get x, <span class="Constant">occupied?:offset</span> |