Multiplication tables: Difference between revisions

m (→‎{{header|Python}}: Another way to convert to 2.X)
Line 100:
<lang cpp>#include <iostream>
#include <iomanip>
#include <cmath> // for log10()
#include <algorithm> // for max()
 
#define MAX(a, b) (a > b ? a : b)
#define ABS(x) (x > 0 ? x : (0 - x) )
 
size_t get_table_column_width(const int min, const int max)
{
unsigned int abs_max = 0std::max(max*max, min*min);
abs_max = MAX(abs_max, ABS(max*max));
abs_max = MAX(abs_max, ABS(max*min));
abs_max = MAX(abs_max, ABS(min*min));
 
// abs_max is the largest absolute value we might see.
Line 116 ⟶ 111:
// of the largest possible absolute value.
// Add one for a little whitespace guarantee.
size_t colwidth = (1 + std::log10(abs_max)) + 1;
 
bool has_negative_result = false;
 
// If bothonly areone of them is less than 0, then all resultssome will be positive
// be negative.
if(!(min < 0) && (max < 0))
bool has_negative_result = false(min < 0) && (max > 0);
{
// If only one of them is less than 0, then some will
// be negative.
if((min < 0) || (max < 0 ))
has_negative_result = true;
}
 
// If some values may be negative, then we need to add some space
// for a sign indicator (-)
if(has_negative_result)
colwidth += 1+;
 
return colwidth;
Anonymous user