SHA256
Biginteger/decimal
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
#include <BigDecimal.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
int main() {
|
||||
using bstl::BigDecimal;
|
||||
|
||||
// 基本构造
|
||||
BigDecimal a("123.45");
|
||||
BigDecimal b("67.89");
|
||||
BigDecimal c = a + b;
|
||||
|
||||
assert(c.toString() == "191.34");
|
||||
|
||||
// 减法
|
||||
BigDecimal d = a - b;
|
||||
assert(d.toString() == "55.56");
|
||||
|
||||
// 乘法
|
||||
BigDecimal e = a * BigDecimal("2");
|
||||
assert(e.toString() == "246.90");
|
||||
|
||||
// 比较
|
||||
assert((a > b) == true);
|
||||
assert((a == a) == true);
|
||||
assert((a != b) == true);
|
||||
|
||||
// 零
|
||||
BigDecimal zero("0.0");
|
||||
assert(zero.isZero());
|
||||
|
||||
// 负数
|
||||
BigDecimal neg("-100.50");
|
||||
assert(neg.sign() < 0);
|
||||
assert((-neg).toString() == "100.50");
|
||||
|
||||
std::cout << "BigDecimal tests passed\n";
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <BigInteger.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
int main() {
|
||||
using bstl::BigInteger;
|
||||
|
||||
BigInteger a("12345678901234567890");
|
||||
BigInteger b("98765432109876543210");
|
||||
BigInteger c = a + b;
|
||||
BigInteger d = b - a;
|
||||
BigInteger e = a * BigInteger("2");
|
||||
BigInteger f = BigInteger("100000000000000000000000") / BigInteger("1000000000000000000");
|
||||
|
||||
assert(c.toString() == "111111111011111111100");
|
||||
assert(d.toString() == "86419753208641975320");
|
||||
assert(e.toString() == "24691357802469135780");
|
||||
assert(f.toString() == "100000");
|
||||
|
||||
BigInteger zero("0");
|
||||
assert((a == a) == true);
|
||||
assert((a != b) == true);
|
||||
assert((zero == 0) == true);
|
||||
assert((a > BigInteger("12345678901234567889")) == true);
|
||||
assert((a < b) == true);
|
||||
|
||||
std::cout << "BigInteger tests passed\n";
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user