Exemplo n.º 1
0
 std::string operator()(Number n) const {
     return "#(" + n.asString() + ")";
 }
Exemplo n.º 2
0
void test_number() {
  cout << "Testing class Number. ";
  //cout << endl;

  Number a = Number(1, 32);
  //cout << a.asString() << endl;
  assert(a.asString() == string("1,0000000000"));

  a += Number(2, 32);
  //cout << a.asString() << endl;
  assert(a.asString() == string("3,0000000000"));

  a += Number(-5, 32);
  //cout << a.asString() << endl;
  assert(a.asString() == string("-2,0000000000"));

  a += Number(2, 32);
  //cout << a.asString() << endl;
  assert(a.asString() == string("-0,00000000000"));

  a += Number(143, 32);
  //cout << a.asString() << endl;
  assert(a.asString() == string("143,00000000"));

  a -= Number(243, 32);
  //cout << a.asString() << endl;
  string b = a.asString();
  //cout << b.length() << endl;
  assert(a.asString() == string("-100,00000000"));

  a /= Number(20, 32);
  //cout << a.asString() << endl;
  assert(a.asString() == string("-5,0000000000"));

  a *= Number(-3, 32);
  //cout << a.asString() << endl;
  assert(a.asString() == string("15,000000000"));

  a -= Number(14, 32);
  a >>= 4;
  //cout << a.asString() << endl;
  assert(a.asString() == string("0,062500000000"));

  a <<= 4;
  //cout << a.asString() << endl;
  assert(a.asString() == string("1,0000000000"));

  a >>= 32;
  //cout << a.asString() << endl;
  assert(a.asString() == string("0,00000000023283064365"));

  a <<= 32;
  //cout << a.asString() << endl;
  assert(a.asString() == string("1,0000000000"));

  Number f = Number(1, dec_to_bin(10));
  f /= 3;
  //cout << "%Rf:     \"" << f.asFormatedString("%Rf") << '\"' << endl;
  assert(f.asFormatedString("%Rf") == string("0.333333"));
  //cout << "%*.Rf:   \"" << f.asFormatedString(10) << '\"' << endl;
  assert(f.asFormatedString(10) == string("0.333333333"));
  //cout << "%*.*Rf:  \"" << f.asFormatedString(
  //    "%*.*Rf", 12, 8) << '\"' << endl;
  assert(f.asFormatedString("%*.*Rf", 12, 8) == string("  0.33333333"));

  f *= 3;
  //cout << "%*.Rf:   \"" << f.asFormatedString(10) << '\"' << endl;
  assert(f.asFormatedString(10) == string("1.000000000"));

  cout << "Ok." << endl;
  }