1

I am trying to do something very simple with GiNaC:

void assert(bool x) {
    if (!x)
        throw runtime_error("Assertion error");
}

#include <ginac/ginac.h>

int main(int argc, char *argv[]) {
    assert(sqrt(ex(32)) == 4 * sqrt(ex(2))); // Raises runtime_error
}

I want to simplify sqrt(32) so that it is equal to 4 * sqrt(2). Of course, from a mathematical standpointm that is the case, but GiNaC does not "realize" this. The documentations mention none of these simplifications and I really don't known whether this is even possible...

HerpDerpington
  • 3,751
  • 4
  • 27
  • 43
  • 2
    Assuming that `sqrt` returns a floating point number, then you shouldn't compare two floating point numbers with an `==` operator, because due to how floating point numbers are stored in computer, they can handle only a limited precision. Read [more](https://floating-point-gui.de/errors/comparison/) on the Internet – Alexey S. Larionov May 22 '20 at 17:36
  • @AlexLarionov I forgot to add something; the `sqrt` functions accept `ex` objects as their argument, which are symbolic expressions. – HerpDerpington May 22 '20 at 17:38
  • 1
    Have you tried `ex(32) == pow(4 * sqrt(ex(2)), 2)`? to see if that works? Just curious if ginac can handle that; it doesn't solve the problem of simplifying `sqrt(ex(32))`. – cigien May 22 '20 at 17:45
  • @cigien That works for some reason... The rhs expression evaluates directly to `ex(32)`. – HerpDerpington May 22 '20 at 18:04
  • But to add to that: This is of course only a minmal example; my actual expressions are much more complicated – HerpDerpington May 22 '20 at 18:10
  • 1
    My *guess* is that `pow` is written to handle that, but `sqrt` doesn't evaluate the way you expect it. Which kind of makes sense, there are probably multiple ways to simplify `sqrt`. Note that I'm just guessing here, I've never worked with ginac before, and there don't appear to be a lot of tutorials or anything. – cigien May 22 '20 at 18:10
  • @cigien Have you worked with similar software and can you [recommend an alternative](https://softwarerecs.stackexchange.com/questions/74627/very-simple-and-fast-cas-in-c)? – HerpDerpington May 22 '20 at 18:14

0 Answers0