-
Notifications
You must be signed in to change notification settings - Fork 5
/
sunumber.h
100 lines (85 loc) · 2.48 KB
/
sunumber.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#pragma once
// Copyright (c) 2000 Suneido Software Corp. All rights reserved
// Licensed under GPLv2
#include "suvalue.h"
#include "dnum.h"
#include "gcstring.h"
class SuNumber : public SuValue {
public:
SuNumber(Dnum d) : dn(d) {
}
explicit SuNumber(int n) : dn(n) {
}
explicit SuNumber(const char* buf) : dn(buf) {
}
static SuNumber* from_int64(int64_t n) {
return new SuNumber(Dnum(n));
}
// handles 0x...
static Value literal(const char* s);
void out(Ostream&) const override;
void* operator new(size_t n); // NOLINT
void* operator new(size_t n, void* p) {
return p;
}
int order() const override;
bool lt(const SuValue& y) const override;
bool eq(const SuValue& y) const override;
friend int cmp(const SuNumber* x, const SuNumber* y) {
return Dnum::cmp(x->dn, y->dn);
}
friend SuNumber* add(const SuNumber* x, const SuNumber* y) {
return new SuNumber(x->dn + y->dn);
}
friend SuNumber* sub(const SuNumber* x, const SuNumber* y) {
return new SuNumber(x->dn - y->dn);
}
friend SuNumber* mul(const SuNumber* x, const SuNumber* y) {
return new SuNumber(x->dn * y->dn);
}
friend SuNumber* div(const SuNumber* x, const SuNumber* y) {
return new SuNumber(x->dn / y->dn);
}
friend SuNumber* neg(const SuNumber* x) {
return new SuNumber(-x->dn);
}
Value call(Value self, Value member, short nargs, short nargnames,
short* argnames, int each) override;
// buf must be larger than mask
char* format(char* buf, const char* mask) const;
SuNumber* number() override {
return this;
}
int integer() const override;
int64_t bigint() const {
return dn.to_int64();
}
int trunc() const;
gcstring to_gcstr() const override {
return dn.to_gcstr();
}
size_t hashfn() const override;
short symnum() const override;
bool int_if_num(int* pn) const override {
return dn.to_int(pn);
}
double to_double() const {
return dn.to_double();
}
static SuNumber* from_double(double x) {
return new SuNumber(Dnum::from_double(x));
}
static SuNumber* from_float(float x) {
return new SuNumber(Dnum::from_double(x));
}
SuNumber* round(int digits, Dnum::RoundingMode mode) const {
return new SuNumber(dn.round(digits, mode));
}
size_t packsize() const override;
void pack(char* buf) const override;
static Value unpack(const gcstring& buf);
static SuNumber zero, one, minus_one, infinity, minus_infinity;
private:
Dnum dn;
};
int numlen(const char* s); // in numlen.cpp