1 #include "mathexpr-ntype.hpp"
2 #include "mathexpr-error.hpp"
3 #include "mathexpr-format.hpp"
12 void throw_domain(const std::string
& err
)
14 throw error(error::WDOMAIN
, err
);
17 template<typename T
> int _cmp_values(T a
, T b
)
28 class expr_val_numeric
38 struct unsigned_tag
{};
41 struct complex_tag
{};
48 expr_val_numeric(unsigned_tag
, uint64_t v
)
54 expr_val_numeric(signed_tag
, int64_t v
)
60 expr_val_numeric(float_tag
, double v
)
66 expr_val_numeric(complex_tag
, double re
, double im
)
72 expr_val_numeric(const std::string
& str
)
78 } else if(regex("[0-9]+|0x[0-9a-fA-F]+", str
)) {
80 v_unsigned
= parse_value
<uint64_t>(str
);
83 } else if(regex("[+-][0-9]+|[+-]0x[0-9a-fA-F]+", str
)) {
85 v_signed
= parse_value
<int64_t>(str
);
88 } else if(regex("[+-]?([0-9]+|[0-9]*\\.[0-9]+)([eE][0-9]+)?", str
)) {
90 v_float
= parse_value
<double>(str
);
94 throw std::runtime_error("Bad number '" + str
+ "'");
96 double as_float() const
99 case T_UNSIGNED
: return v_unsigned
;
100 case T_SIGNED
: return v_signed
;
101 case T_FLOAT
: return v_float
;
102 case T_COMPLEX
: return v_float
;
104 throw error(error::INTERNAL
, "Internal error (shouldn't be here)");
106 int64_t as_signed() const
109 case T_UNSIGNED
: return v_unsigned
;
110 case T_SIGNED
: return v_signed
;
111 case T_FLOAT
: return v_float
;
112 case T_COMPLEX
: return v_float
;
114 throw error(error::INTERNAL
, "Internal error (shouldn't be here)");
116 uint64_t as_unsigned() const
119 case T_UNSIGNED
: return v_unsigned
;
120 case T_SIGNED
: return v_signed
;
121 case T_FLOAT
: return v_float
;
122 case T_COMPLEX
: return v_float
;
124 throw error(error::INTERNAL
, "Internal error (shouldn't be here)");
126 std::string
tostring()
130 return (stringfmt() << v_unsigned
).str();
132 return (stringfmt() << v_signed
).str();
134 //FIXME: Saner formatting.
135 return (stringfmt() << v_float
).str();
137 //FIXME: Saner formatting.
139 return (stringfmt() << v_float
<< v_imag
<< "*i").str();
141 return (stringfmt() << v_float
<< "+" << v_imag
<< "*i").str();
142 return (stringfmt() << v_float
).str();
144 throw error(error::INTERNAL
, "Internal error (shouldn't be here)");
146 uint64_t tounsigned()
148 return as_unsigned();
154 void scale(uint64_t scale
)
159 v_float
= (1.0 * v_unsigned
/ scale
);
163 v_float
= (1.0 * v_signed
/ scale
);
175 case T_UNSIGNED
: return (v_unsigned
!= 0);
176 case T_SIGNED
: return (v_signed
!= 0);
177 case T_FLOAT
: return (v_float
!= 0);
178 case T_COMPLEX
: return (v_float
!= 0) || (v_imag
!= 0);
180 throw error(error::INTERNAL
, "Internal error (shouldn't be here)");
182 std::string
format(_format fmt
)
185 case T_UNSIGNED
: return format_unsigned(v_unsigned
, fmt
);
186 case T_SIGNED
: return format_signed(v_signed
, fmt
);
187 case T_FLOAT
: return format_float(v_float
, fmt
);
188 case T_COMPLEX
: return format_complex(v_float
, v_imag
, fmt
);
190 throw error(error::INTERNAL
, "Don't know how to print numeric type");
192 expr_val_numeric
operator~() const
195 case T_UNSIGNED
: return expr_val_numeric(unsigned_tag(), ~v_unsigned
);
196 case T_SIGNED
: return expr_val_numeric(signed_tag(), ~v_signed
);
197 case T_FLOAT
: throw_domain("Bit operations are only for integers");
198 case T_COMPLEX
: throw_domain("Bit operations are only for integers");
200 throw error(error::INTERNAL
, "Internal error (shouldn't be here)");
202 expr_val_numeric
operator&(const expr_val_numeric
& b
) const
204 if(type
== T_COMPLEX
|| b
.type
== T_COMPLEX
)
205 throw_domain("Bit operations are only for integers");
206 if(type
== T_FLOAT
|| b
.type
== T_FLOAT
)
207 throw_domain("Bit operations are only for integers");
208 if(type
== T_SIGNED
|| b
.type
== T_SIGNED
)
209 return expr_val_numeric(signed_tag(), as_signed() & b
.as_signed());
210 return expr_val_numeric(unsigned_tag(), as_unsigned() & b
.as_unsigned());
212 expr_val_numeric
operator|(const expr_val_numeric
& b
) const
214 if(type
== T_COMPLEX
|| b
.type
== T_COMPLEX
)
215 throw_domain("Bit operations are only for integers");
216 if(type
== T_FLOAT
|| b
.type
== T_FLOAT
)
217 throw_domain("Bit operations are only for integers");
218 if(type
== T_SIGNED
|| b
.type
== T_SIGNED
)
219 return expr_val_numeric(signed_tag(), as_signed() | b
.as_signed());
220 return expr_val_numeric(unsigned_tag(), as_unsigned() | b
.as_unsigned());
222 expr_val_numeric
operator^(const expr_val_numeric
& b
) const
224 if(type
== T_COMPLEX
|| b
.type
== T_COMPLEX
)
225 throw_domain("Bit operations are only for integers");
226 if(type
== T_FLOAT
|| b
.type
== T_FLOAT
)
227 throw_domain("Bit operations are only for integers");
228 if(type
== T_SIGNED
|| b
.type
== T_SIGNED
)
229 return expr_val_numeric(signed_tag(), as_signed() ^ b
.as_signed());
230 return expr_val_numeric(unsigned_tag(), as_unsigned() ^ b
.as_unsigned());
232 expr_val_numeric
operator-() const
235 case T_COMPLEX
: return expr_val_numeric(complex_tag(), -v_float
, -v_imag
);
236 case T_UNSIGNED
: return expr_val_numeric(signed_tag(), -(int64_t)v_unsigned
);
237 case T_SIGNED
: return expr_val_numeric(signed_tag(), -v_signed
);
238 case T_FLOAT
: return expr_val_numeric(float_tag(), -v_float
);
240 throw error(error::INTERNAL
, "Internal error (shouldn't be here)");
242 expr_val_numeric
operator+(const expr_val_numeric
& b
) const
244 if(type
== T_COMPLEX
|| b
.type
== T_COMPLEX
)
245 return expr_val_numeric(complex_tag(), as_float() + b
.as_float(),
247 if(type
== T_FLOAT
|| b
.type
== T_FLOAT
)
248 return expr_val_numeric(float_tag(), as_float() + b
.as_float());
249 if(type
== T_SIGNED
|| b
.type
== T_SIGNED
)
250 return expr_val_numeric(signed_tag(), as_signed() + b
.as_signed());
251 return expr_val_numeric(unsigned_tag(), as_unsigned() + b
.as_unsigned());
253 expr_val_numeric
operator-(const expr_val_numeric
& b
) const
255 if(type
== T_COMPLEX
|| b
.type
== T_COMPLEX
)
256 return expr_val_numeric(complex_tag(), as_float() - b
.as_float(),
258 if(type
== T_FLOAT
|| b
.type
== T_FLOAT
)
259 return expr_val_numeric(float_tag(), as_float() - b
.as_float());
260 if(type
== T_SIGNED
|| b
.type
== T_SIGNED
)
261 return expr_val_numeric(signed_tag(), as_signed() - b
.as_signed());
262 return expr_val_numeric(unsigned_tag(), as_unsigned() - b
.as_unsigned());
264 expr_val_numeric
operator*(const expr_val_numeric
& b
) const
266 if(type
== T_COMPLEX
|| b
.type
== T_COMPLEX
)
267 return expr_val_numeric(complex_tag(), as_float() * b
.as_float() - v_imag
* b
.v_imag
,
268 as_float() * b
.v_imag
+ b
.as_float() * v_imag
);
269 if(type
== T_FLOAT
|| b
.type
== T_FLOAT
)
270 return expr_val_numeric(float_tag(), as_float() * b
.as_float());
271 if(type
== T_SIGNED
|| b
.type
== T_SIGNED
)
272 return expr_val_numeric(signed_tag(), as_signed() * b
.as_signed());
273 return expr_val_numeric(unsigned_tag(), as_unsigned() * b
.as_unsigned());
275 expr_val_numeric
operator/(const expr_val_numeric
& b
) const
277 if(type
== T_COMPLEX
|| b
.type
== T_COMPLEX
) {
278 double div
= b
.as_float() * b
.as_float() + b
.v_imag
* b
.v_imag
;
280 throw error(error::DIV_BY_0
, "Division by 0");
281 return expr_val_numeric(complex_tag(),
282 (as_float() * b
.as_float() + v_imag
* b
.v_imag
) / div
,
283 (v_imag
* b
.as_float() - as_float() * b
.v_imag
) / div
);
285 if(type
== T_FLOAT
|| b
.type
== T_FLOAT
) {
286 if(b
.as_float() == 0)
287 throw error(error::DIV_BY_0
, "Division by 0");
288 return expr_val_numeric(float_tag(), as_float() / b
.as_float());
290 if(type
== T_SIGNED
|| b
.type
== T_SIGNED
) {
291 if(b
.as_signed() == 0)
292 throw error(error::DIV_BY_0
, "Division by 0");
293 return expr_val_numeric(signed_tag(), as_signed() / b
.as_signed());
295 if(b
.as_unsigned() == 0)
296 throw error(error::DIV_BY_0
, "Division by 0");
297 return expr_val_numeric(unsigned_tag(), as_unsigned() / b
.as_unsigned());
299 expr_val_numeric
operator%(const expr_val_numeric
& b
) const
301 if(type
== T_COMPLEX
|| b
.type
== T_COMPLEX
)
302 throw_domain("Remainder is only for integers");
303 if(type
== T_FLOAT
|| b
.type
== T_FLOAT
)
304 throw_domain("Remainder is only for integers");
305 if(type
== T_SIGNED
|| b
.type
== T_SIGNED
) {
306 if(b
.as_signed() == 0)
307 throw error(error::DIV_BY_0
, "Division by 0");
308 return expr_val_numeric(signed_tag(), as_signed() / b
.as_signed());
310 if(b
.as_unsigned() == 0)
311 throw error(error::DIV_BY_0
, "Division by 0");
312 return expr_val_numeric(unsigned_tag(), as_unsigned() / b
.as_unsigned());
314 expr_val_numeric
log() const
316 if(type
== T_COMPLEX
) {
317 double mag
= as_float() * as_float() + v_imag
* v_imag
;
319 throw error(error::LOG_BY_0
, "Can't take logarithm of 0");
320 double r
= 0.5 * ::log(mag
);
321 double i
= ::atan2(v_imag
, as_float());
322 return expr_val_numeric(complex_tag(), r
, i
);
325 throw error(error::LOG_BY_0
, "Can't take logarithm of 0");
327 return expr_val_numeric(complex_tag(), ::log(std::abs(as_float())), 4 * ::atan(1));
328 return expr_val_numeric(float_tag(), ::log(as_float()));
330 static expr_val_numeric
log2(expr_val_numeric a
, expr_val_numeric b
)
332 return b
.log() / a
.log();
334 expr_val_numeric
exp() const
336 if(type
== T_COMPLEX
) {
337 double mag
= ::exp(as_float());
338 return expr_val_numeric(complex_tag(), mag
* ::cos(v_imag
),
339 mag
* ::sin(v_imag
));
341 return expr_val_numeric(float_tag(), ::exp(as_float()));
343 static expr_val_numeric
exp2(expr_val_numeric a
, expr_val_numeric b
)
345 expr_val_numeric tmp
= b
* a
.log();
348 expr_val_numeric
sqrt() const
350 if(as_float() < 0 && type
!= T_COMPLEX
)
351 return expr_val_numeric(complex_tag(), 0, ::sqrt(-as_float()));
352 if(type
== T_COMPLEX
) {
353 double mag
= ::sqrt(::sqrt(as_float() * as_float() + v_imag
* v_imag
));
354 double ar
= 0.5 * ::atan2(v_imag
, as_float());
355 return expr_val_numeric(complex_tag(), mag
* ::cos(ar
), mag
* ::sin(ar
));
357 return expr_val_numeric(float_tag(), ::sqrt(as_float()));
359 expr_val_numeric
sin() const
361 if(type
== T_COMPLEX
) {
362 return expr_val_numeric(complex_tag(), ::sin(as_float()) * ::cosh(v_imag
),
363 ::cos(as_float()) * ::sinh(v_imag
));
365 return expr_val_numeric(float_tag(), ::sin(as_float()));
367 expr_val_numeric
cos() const
369 if(type
== T_COMPLEX
) {
370 return expr_val_numeric(complex_tag(), ::cos(as_float()) * ::cosh(v_imag
),
371 -::sin(as_float()) * ::sinh(v_imag
));
373 return expr_val_numeric(float_tag(), ::cos(as_float()));
375 expr_val_numeric
tan() const
379 expr_val_numeric
atan() const
381 if(type
== T_COMPLEX
) {
382 expr_val_numeric x
= expr_val_numeric(complex_tag(), 0, 1) * *this;
383 expr_val_numeric n
= expr_val_numeric(complex_tag(), 1, 0) + x
;
384 expr_val_numeric d
= expr_val_numeric(complex_tag(), 1, 0) - x
;
385 expr_val_numeric y
= n
/ d
;
386 expr_val_numeric w
= y
.log();
387 return w
/ expr_val_numeric(complex_tag(), 0, 2);
389 return expr_val_numeric(float_tag(), ::atan(as_float()));
391 expr_val_numeric
acos() const
393 expr_val_numeric sinesqr
= (expr_val_numeric(float_tag(), 1) - *this * *this);
394 expr_val_numeric sine
= sinesqr
.sqrt();
395 expr_val_numeric tangent
= sine
/ *this;
396 return tangent
.atan();
398 expr_val_numeric
asin() const
400 expr_val_numeric cosinesqr
= (expr_val_numeric(float_tag(), 1) - *this * *this);
401 expr_val_numeric cosine
= cosinesqr
.sqrt();
402 expr_val_numeric tangent
= *this / cosine
;
403 return tangent
.atan();
405 expr_val_numeric
sinh() const
407 return (exp() - (-*this).exp()) / expr_val_numeric(float_tag(), 2);
409 expr_val_numeric
cosh() const
411 return (exp() + (-*this).exp()) / expr_val_numeric(float_tag(), 2);
413 expr_val_numeric
tanh() const
415 return sinh() / cosh();
417 expr_val_numeric
arsinh() const
421 //(x-u)^2 - x^2 + 2ux - u^2 + x^2 - 2ux - 1 = 0
423 expr_val_numeric xmu
= (*this * *this) + expr_val_numeric(float_tag(), 1);
424 expr_val_numeric x
= xmu
.sqrt() + *this;
427 expr_val_numeric
arcosh() const
429 expr_val_numeric xmu
= (*this * *this) - expr_val_numeric(float_tag(), 1);
430 expr_val_numeric x
= xmu
.sqrt() + *this;
433 expr_val_numeric
artanh() const
437 expr_val_numeric
t(float_tag(), 1);
438 return ((t
+ *this) / (t
- *this)).sqrt().log();
440 static expr_val_numeric
atan2(expr_val_numeric a
, expr_val_numeric b
)
442 if(a
.type
== T_COMPLEX
|| b
.type
== T_COMPLEX
)
443 throw_domain("atan2 is only for reals");
444 return expr_val_numeric(float_tag(), ::atan2(a
.as_float(), b
.as_float()));
446 expr_val_numeric
torad() const
448 return expr_val_numeric(float_tag(), ::atan(1) / 45 * as_float());
450 expr_val_numeric
todeg() const
452 return expr_val_numeric(float_tag(), 45 / ::atan(1) * as_float());
454 static expr_val_numeric
shift(expr_val_numeric a
, expr_val_numeric b
, bool inv
)
456 int64_t s
= b
.as_signed();
459 if(a
.type
== T_SIGNED
) {
460 int64_t _a
= a
.v_signed
;
462 return expr_val_numeric(signed_tag(), -1);
464 return expr_val_numeric(signed_tag(), 0);
467 uint64_t r2
= r
>> -s
;
468 uint64_t m
= 0xFFFFFFFFFFFFFFFFULL
- ((1ULL << (64 - s
)) - 1);
469 return expr_val_numeric(signed_tag(), m
| r2
);
471 return expr_val_numeric(signed_tag(), _a
<< s
);
473 return expr_val_numeric(signed_tag(), _a
);
474 } else if(a
.type
== T_UNSIGNED
) {
475 uint64_t _a
= a
.v_unsigned
;
476 if(s
< -63 || s
> 63)
477 return expr_val_numeric(unsigned_tag(), 0);
479 return expr_val_numeric(unsigned_tag(), _a
>> -s
);
481 return expr_val_numeric(unsigned_tag(), _a
<< s
);
483 return expr_val_numeric(unsigned_tag(), _a
);
485 throw_domain("Bit operations are only for integers");
486 return expr_val_numeric(unsigned_tag(), 0); //NOTREACHED
488 static expr_val_numeric
op_pi()
490 return expr_val_numeric(float_tag(), 4 * ::atan(1));
492 static expr_val_numeric
op_e()
494 return expr_val_numeric(float_tag(), ::exp(1));
496 bool operator==(const expr_val_numeric
& b
) const
498 if(type
== T_COMPLEX
|| b
.type
== T_COMPLEX
)
499 return as_float() == b
.as_float() && v_imag
== b
.v_imag
;
500 return (_cmp(*this, b
) == 0);
502 static int _cmp_float_unsigned(uint64_t a
, float b
)
506 //TODO: Handle values too large for exact integer representation.
513 static int _cmp_float_signed(int64_t a
, float b
)
515 //TODO: Handle values too large for exact integer representation.
522 static int _cmp(expr_val_numeric a
, expr_val_numeric b
)
524 if(a
.type
== T_COMPLEX
|| b
.type
== T_COMPLEX
)
525 throw_domain("Can't compare complex numbers");
530 return _cmp_values(a
.v_unsigned
, b
.v_unsigned
);
534 if((int64_t)a
.v_unsigned
< 0)
536 return _cmp_values((int64_t)a
.v_unsigned
, b
.v_signed
);
538 return _cmp_float_unsigned(a
.v_unsigned
, b
.v_float
);
540 throw error(error::INTERNAL
,
541 "Internal error (shouldn't be here)");
548 if((int64_t)b
.v_unsigned
< 0)
550 return _cmp_values(a
.v_signed
, (int64_t)b
.v_unsigned
);
552 return _cmp_values(a
.v_signed
, b
.v_signed
);
554 return _cmp_float_signed(a
.v_signed
, b
.v_float
);
556 throw error(error::INTERNAL
,
557 "Internal error (shouldn't be here)");
562 return -_cmp_float_unsigned(b
.v_unsigned
, a
.v_float
);
564 return -_cmp_float_signed(b
.v_signed
, a
.v_float
);
566 if(a
.v_float
< b
.v_float
)
568 if(a
.v_float
> b
.v_float
)
572 throw error(error::INTERNAL
,
573 "Internal error (shouldn't be here)");
576 throw error(error::INTERNAL
, "Internal error (shouldn't be here)");
578 throw error(error::INTERNAL
, "Internal error (shouldn't be here)");
580 static expr_val_numeric
x_unsigned(expr_val_numeric a
)
583 case T_UNSIGNED
: return expr_val_numeric(unsigned_tag(), a
.v_unsigned
);
584 case T_SIGNED
: return expr_val_numeric(unsigned_tag(), a
.v_signed
);
585 case T_FLOAT
: return expr_val_numeric(unsigned_tag(), a
.v_float
);
586 default: throw_domain("Can't convert non-real into unsigned");
588 return expr_val_numeric(unsigned_tag(), 0); //NOTREACHED.
590 static expr_val_numeric
x_signed(expr_val_numeric a
)
593 case T_UNSIGNED
: return expr_val_numeric(signed_tag(), a
.v_unsigned
);
594 case T_SIGNED
: return expr_val_numeric(signed_tag(), a
.v_signed
);
595 case T_FLOAT
: return expr_val_numeric(signed_tag(), a
.v_float
);
596 default: throw_domain("Can't convert non-real into signed");
598 return expr_val_numeric(signed_tag(), 0); //NOTREACHED.
600 static expr_val_numeric
x_float(expr_val_numeric a
)
603 case T_UNSIGNED
: return expr_val_numeric(float_tag(), a
.v_unsigned
);
604 case T_SIGNED
: return expr_val_numeric(float_tag(), a
.v_signed
);
605 case T_FLOAT
: return expr_val_numeric(float_tag(), a
.v_float
);
606 default: throw_domain("Can't convert non-real into float");
608 return expr_val_numeric(float_tag(), 0); //NOTREACHED.
610 expr_val_numeric
re() const
612 if(type
== T_COMPLEX
)
613 return expr_val_numeric(float_tag(), v_float
);
614 return expr_val_numeric(float_tag(), as_float());
616 expr_val_numeric
im() const
618 if(type
== T_COMPLEX
)
619 return expr_val_numeric(float_tag(), v_imag
);
620 return expr_val_numeric(float_tag(), 0);
622 expr_val_numeric
conj() const
624 if(type
== T_COMPLEX
)
625 return expr_val_numeric(complex_tag(), v_float
, -v_imag
);
626 return expr_val_numeric(float_tag(), as_float());
628 expr_val_numeric
abs() const
631 case T_COMPLEX
: return expr_val_numeric(float_tag(), v_float
* v_float
+ v_imag
* v_imag
);
632 case T_FLOAT
: return expr_val_numeric(float_tag(), ::fabs(v_float
));
633 case T_SIGNED
: return expr_val_numeric(signed_tag(), ::abs(v_signed
));
634 case T_UNSIGNED
: return expr_val_numeric(unsigned_tag(), v_unsigned
);
636 throw error(error::INTERNAL
, "Internal error (shouldn't be here)");
638 expr_val_numeric
arg() const
641 case T_COMPLEX
: return expr_val_numeric(float_tag(), ::atan2(v_imag
, v_float
));
644 return expr_val_numeric(float_tag(), 4 * ::atan(1));
646 return expr_val_numeric(float_tag(), 0);
661 struct number_tag
{};
662 struct string_tag
{};
663 struct boolean_tag
{};
670 expr_val_numeric
& as_numeric()
672 if(type
!= T_NUMERIC
)
673 throw_domain("Can't operate with non-numbers");
681 expr_val(const std::string
& str
, bool string
)
686 } else if(str
== "false") {
689 } else if(str
== "true") {
692 } else if(str
== "e") {
693 v_numeric
= expr_val_numeric::op_e();
695 } else if(str
== "pi") {
696 v_numeric
= expr_val_numeric::op_pi();
699 v_numeric
= expr_val_numeric(str
);
703 expr_val(typeinfo_wrapper
<expr_val
>::unsigned_tag t
, uint64_t v
)
704 : type(T_NUMERIC
), v_numeric(expr_val_numeric::unsigned_tag(), v
)
707 expr_val(typeinfo_wrapper
<expr_val
>::signed_tag t
, int64_t v
)
708 : type(T_NUMERIC
), v_numeric(expr_val_numeric::signed_tag(), v
)
711 expr_val(typeinfo_wrapper
<expr_val
>::float_tag t
, double v
)
712 : type(T_NUMERIC
), v_numeric(expr_val_numeric::float_tag(), v
)
715 expr_val(typeinfo_wrapper
<expr_val
>::boolean_tag
, bool b
)
716 : type(T_BOOLEAN
), v_boolean(b
)
719 expr_val(expr_val_numeric v
)
720 : type(T_NUMERIC
), v_numeric(v
)
723 expr_val(string_tag
, std::string s
)
724 : type(T_STRING
), v_string(s
)
727 expr_val(boolean_tag
, bool b
)
728 : type(T_BOOLEAN
), v_boolean(b
)
731 std::string
tostring()
740 return v_numeric
.tostring();
744 throw error(error::INTERNAL
, "Internal error (shouldn't be here)");
746 uint64_t tounsigned()
748 if(type
!= T_NUMERIC
)
749 throw_domain("Can't convert non-number into unsigned");
750 return v_numeric
.tounsigned();
754 if(type
!= T_NUMERIC
)
755 throw_domain("Can't convert non-number into signed");
756 return v_numeric
.tosigned();
758 void scale(uint64_t _scale
)
760 if(type
!= T_NUMERIC
)
761 throw_domain("Can't scale non-number");
762 v_numeric
.scale(_scale
);
770 return v_numeric
.toboolean();
772 return (v_string
.length() != 0);
774 throw error(error::INTERNAL
, "Internal error (shouldn't be here)");
776 static expr_val
op_lnot(std::vector
<std::function
<expr_val
&()>> promises
)
778 if(promises
.size() != 1)
779 throw error(error::ARGCOUNT
, "logical not takes 1 argument");
780 return expr_val(boolean_tag(), !(promises
[0]().toboolean()));
782 static expr_val
op_lor(std::vector
<std::function
<expr_val
&()>> promises
)
784 if(promises
.size() != 2)
785 throw error(error::ARGCOUNT
, "logical or takes 2 arguments");
786 if(promises
[0]().toboolean())
787 return expr_val(boolean_tag(), true);
788 return expr_val(boolean_tag(), promises
[1]().toboolean());
790 static expr_val
op_land(std::vector
<std::function
<expr_val
&()>> promises
)
792 if(promises
.size() != 2)
793 throw error(error::ARGCOUNT
, "logical and takes 2 arguments");
794 if(!(promises
[0]().toboolean()))
795 return expr_val(boolean_tag(), false);
796 return expr_val(boolean_tag(), promises
[1]().toboolean());
798 static expr_val
fun_if(std::vector
<std::function
<expr_val
&()>> promises
)
800 if(promises
.size() == 2) {
801 if((promises
[0]().toboolean()))
802 return promises
[1]();
804 return expr_val(boolean_tag(), false);
805 } else if(promises
.size() == 3) {
806 if((promises
[0]().toboolean()))
807 return promises
[1]();
809 return promises
[2]();
811 throw error(error::ARGCOUNT
, "if takes 2 or 3 arguments");
813 static expr_val
fun_select(std::vector
<std::function
<expr_val
&()>> promises
)
815 for(auto& i
: promises
) {
817 if(v
.type
!= T_BOOLEAN
|| v
.v_boolean
)
820 return expr_val(boolean_tag(), false);
822 static expr_val
fun_pyth(std::vector
<std::function
<expr_val
&()>> promises
)
824 std::vector
<expr_val
> v
;
825 for(auto& i
: promises
)
827 expr_val_numeric
n(expr_val_numeric::float_tag(), 0);
828 expr_val_numeric
one(expr_val_numeric::float_tag(), 1);
830 if(i
.type
!= T_NUMERIC
)
831 throw error(error::WDOMAIN
, "pyth requires numeric args");
832 n
= n
+ one
* i
.v_numeric
* i
.v_numeric
;
836 template<expr_val (*T
)(expr_val
& a
, expr_val
& b
)>
837 static expr_val
fun_fold(std::vector
<std::function
<expr_val
&()>> promises
)
840 return expr_val(boolean_tag(), false);
841 expr_val v
= promises
[0]();
842 for(size_t i
= 1; i
< promises
.size(); i
++)
843 v
= T(v
, promises
[i
]());
846 static expr_val
fold_min(expr_val
& a
, expr_val
& b
)
848 int t
= _cmp_values(a
.type
, b
.type
);
853 return (_cmp(a
, b
) < 0) ? a
: b
;
855 static expr_val
fold_max(expr_val
& a
, expr_val
& b
)
857 int t
= _cmp_values(a
.type
, b
.type
);
862 return (_cmp(a
, b
) > 0) ? a
: b
;
864 static expr_val
fold_sum(expr_val
& a
, expr_val
& b
)
868 static expr_val
fold_prod(expr_val
& a
, expr_val
& b
)
872 template<expr_val (*T
)(expr_val a
, expr_val b
)>
873 static expr_val
op_binary(std::vector
<std::function
<expr_val
&()>> promises
)
875 if(promises
.size() != 2)
876 throw error(error::ARGCOUNT
, "Operation takes 2 arguments");
877 expr_val a
= promises
[0]();
878 expr_val b
= promises
[1]();
881 template<expr_val (*T
)(expr_val a
)>
882 static expr_val
op_unary(std::vector
<std::function
<expr_val
&()>> promises
)
884 if(promises
.size() != 1)
885 throw error(error::ARGCOUNT
, "Operation takes 1 argument");
886 expr_val a
= promises
[0]();
889 template<expr_val (*T
)(expr_val a
),expr_val (*U
)(expr_val a
, expr_val b
)>
890 static expr_val
op_unary_binary(std::vector
<std::function
<expr_val
&()>> promises
)
892 if(promises
.size() == 1)
893 return T(promises
[0]());
894 if(promises
.size() == 2)
895 return U(promises
[0](), promises
[1]());
896 throw error(error::ARGCOUNT
, "Operation takes 1 or 2 arguments");
898 static expr_val
bnot(expr_val a
)
900 return ~a
.as_numeric();
902 static expr_val
band(expr_val a
, expr_val b
)
904 return a
.as_numeric() & b
.as_numeric();
906 static expr_val
bor(expr_val a
, expr_val b
)
908 return a
.as_numeric() | b
.as_numeric();
910 static expr_val
bxor(expr_val a
, expr_val b
)
912 return a
.as_numeric() ^ b
.as_numeric();
914 static expr_val
neg(expr_val a
)
916 return -a
.as_numeric();
918 template<expr_val_numeric (expr_val_numeric::*T
)() const>
919 static expr_val
f_n_fn(expr_val a
)
921 return (a
.as_numeric().*T
)();
923 template<expr_val_numeric (*T
)(expr_val_numeric x
, expr_val_numeric y
)>
924 static expr_val
f_n_fn2(expr_val a
, expr_val b
)
926 return T(a
.as_numeric(), b
.as_numeric());
928 static expr_val
lshift(expr_val a
, expr_val b
)
930 return expr_val_numeric::shift(a
.as_numeric(), b
.as_numeric(), false);
932 static expr_val
rshift(expr_val a
, expr_val b
)
934 return expr_val_numeric::shift(a
.as_numeric(), b
.as_numeric(), true);
936 static expr_val
op_pi(std::vector
<std::function
<expr_val
&()>> promises
)
938 return expr_val_numeric::op_pi();
940 static expr_val
add(expr_val a
, expr_val b
)
942 if(a
.type
== T_STRING
&& b
.type
== T_STRING
)
943 return expr_val(string_tag(), a
.v_string
+ b
.v_string
);
944 return a
.as_numeric() + b
.as_numeric();
946 static expr_val
sub(expr_val a
, expr_val b
)
948 return a
.as_numeric() - b
.as_numeric();
950 static expr_val
mul(expr_val a
, expr_val b
)
952 return a
.as_numeric() * b
.as_numeric();
954 static expr_val
div(expr_val a
, expr_val b
)
956 return a
.as_numeric() / b
.as_numeric();
958 static expr_val
rem(expr_val a
, expr_val b
)
960 return a
.as_numeric() % b
.as_numeric();
962 static bool _eq(const expr_val
& a
, const expr_val
& b
)
967 case T_BOOLEAN
: return (a
.v_boolean
== b
.v_boolean
);
968 case T_STRING
: return (a
.v_string
== b
.v_string
);
969 case T_NUMERIC
: return (a
.v_numeric
== b
.v_numeric
);
971 throw error(error::INTERNAL
, "Internal error (shouldn't be here)");
973 static expr_val
eq(expr_val a
, expr_val b
)
975 return expr_val(boolean_tag(), _eq(a
, b
));
977 static expr_val
ne(expr_val a
, expr_val b
)
979 return expr_val(boolean_tag(), !_eq(a
, b
));
981 static int _cmp(expr_val a
, expr_val b
)
984 throw_domain("Can't compare distinct value types");
986 case T_BOOLEAN
: return _cmp_values(a
.v_boolean
, b
.v_boolean
);
987 case T_STRING
: return _cmp_values(a
.v_string
, b
.v_string
);
988 case T_NUMERIC
: return expr_val_numeric::_cmp(a
.v_numeric
, b
.v_numeric
);
990 throw error(error::INTERNAL
, "Internal error (shouldn't be here)");
992 template<expr_val_numeric(*T
)(expr_val_numeric v
)>
993 static expr_val
x_nconv(expr_val a
)
995 return T(a
.as_numeric());
997 static expr_val
lt(expr_val a
, expr_val b
)
999 return expr_val(boolean_tag(), _cmp(a
, b
) < 0);
1001 static expr_val
le(expr_val a
, expr_val b
)
1003 return expr_val(boolean_tag(), _cmp(a
, b
) <= 0);
1005 static expr_val
ge(expr_val a
, expr_val b
)
1007 return expr_val(boolean_tag(), _cmp(a
, b
) >= 0);
1009 static expr_val
gt(expr_val a
, expr_val b
)
1011 return expr_val(boolean_tag(), _cmp(a
, b
) > 0);
1013 std::string
format_string(std::string val
, _format fmt
)
1015 if((int)val
.length() > fmt
.precision
&& fmt
.precision
>= 0)
1016 val
= val
.substr(0, fmt
.precision
);
1017 while((int)val
.length() < fmt
.width
)
1021 std::string
print_bool_numeric(bool val
, _format fmt
)
1023 std::string out
= val
? "1" : "0";
1024 if(fmt
.precision
> 0) {
1026 for(int i
= 0; i
< fmt
.precision
; i
++)
1029 while((int)out
.length() < fmt
.width
)
1030 out
= ((fmt
.fillzeros
) ? "0" : " ") + out
;
1033 std::string
format(_format fmt
)
1036 case T_BOOLEAN
: return format_bool(v_boolean
, fmt
);
1037 case T_NUMERIC
: return v_numeric
.format(fmt
);
1038 case T_STRING
: return format_string(v_string
, fmt
);
1040 return "#Notprintable";
1043 static std::set
<operinfo
*> operations()
1045 static operinfo_set
<expr_val
> x({
1046 {"-", expr_val::op_unary
<expr_val::neg
>, true, 1, -3, true},
1047 {"!", expr_val::op_lnot
, true, 1, -3, true},
1048 {"~", expr_val::op_unary
<expr_val::bnot
>, true, 1, -3, true},
1049 {"*", expr_val::op_binary
<expr_val::mul
>, true, 2, -5, false},
1050 {"/", expr_val::op_binary
<expr_val::div
>, true, 2, -5, false},
1051 {"%", expr_val::op_binary
<expr_val::rem
>, true, 2, -5, false},
1052 {"+", expr_val::op_binary
<expr_val::add
>, true, 2, -6, false},
1053 {"-", expr_val::op_binary
<expr_val::sub
>, true, 2, -6, false},
1054 {"<<", expr_val::op_binary
<expr_val::lshift
>, true, 2, -7, false},
1055 {">>", expr_val::op_binary
<expr_val::rshift
>, true, 2, -7, false},
1056 {"<", expr_val::op_binary
<expr_val::lt
>, true, 2, -8, false},
1057 {"<=", expr_val::op_binary
<expr_val::le
>, true, 2, -8, false},
1058 {">", expr_val::op_binary
<expr_val::gt
>, true, 2, -8, false},
1059 {">=", expr_val::op_binary
<expr_val::ge
>, true, 2, -8, false},
1060 {"==", expr_val::op_binary
<expr_val::eq
>, true, 2, -9, false},
1061 {"!=", expr_val::op_binary
<expr_val::ne
>, true, 2, -9, false},
1062 {"&", expr_val::op_binary
<expr_val::band
>, true, 2, -10, false},
1063 {"^", expr_val::op_binary
<expr_val::bxor
>, true, 2, -11, false},
1064 {"|", expr_val::op_binary
<expr_val::bor
>, true, 2, -12, false},
1065 {"&&", expr_val::op_land
, true, 2, -13, false},
1066 {"||", expr_val::op_lor
, true, 2, -14, false},
1067 {"π", expr_val::op_pi
, true, 0, 0, false},
1068 {"if", expr_val::fun_if
},
1069 {"select", expr_val::fun_select
},
1070 {"unsigned", expr_val::op_unary
<expr_val::x_nconv
<expr_val_numeric::x_unsigned
>>},
1071 {"signed", expr_val::op_unary
<expr_val::x_nconv
<expr_val_numeric::x_signed
>>},
1072 {"float", expr_val::op_unary
<expr_val::x_nconv
<expr_val_numeric::x_float
>>},
1073 {"min", expr_val::fun_fold
<expr_val::fold_min
>},
1074 {"max", expr_val::fun_fold
<expr_val::fold_max
>},
1075 {"sum", expr_val::fun_fold
<expr_val::fold_sum
>},
1076 {"prod", expr_val::fun_fold
<expr_val::fold_prod
>},
1077 {"sqrt", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::sqrt
>>},
1078 {"log", expr_val::op_unary_binary
<expr_val::f_n_fn
<&expr_val_numeric::log
>,
1079 expr_val::f_n_fn2
<expr_val_numeric::log2
>>},
1080 {"exp", expr_val::op_unary_binary
<expr_val::f_n_fn
<&expr_val_numeric::exp
>,
1081 expr_val::f_n_fn2
<expr_val_numeric::exp2
>>},
1082 {"sin", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::sin
>>},
1083 {"cos", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::cos
>>},
1084 {"tan", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::tan
>>},
1085 {"atan", expr_val::op_unary_binary
<expr_val::f_n_fn
<&expr_val_numeric::atan
>,
1086 expr_val::f_n_fn2
<expr_val_numeric::atan2
>>},
1087 {"asin", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::asin
>>},
1088 {"acos", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::acos
>>},
1089 {"sinh", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::sinh
>>},
1090 {"cosh", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::cosh
>>},
1091 {"tanh", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::tanh
>>},
1092 {"artanh", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::artanh
>>},
1093 {"arsinh", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::arsinh
>>},
1094 {"arcosh", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::arcosh
>>},
1095 {"torad", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::torad
>>},
1096 {"todeg", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::todeg
>>},
1097 {"re", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::re
>>},
1098 {"im", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::im
>>},
1099 {"conj", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::conj
>>},
1100 {"abs", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::abs
>>},
1101 {"arg", expr_val::op_unary
<expr_val::f_n_fn
<&expr_val_numeric::arg
>>},
1102 {"pyth", expr_val::fun_pyth
},
1109 expr_val_numeric v_numeric
;
1110 std::string v_string
;
1114 struct typeinfo
* expression_value()
1116 static typeinfo_wrapper
<expr_val
> expession_value_obj
;
1117 return &expession_value_obj
;