 Posts: 23951
Joined: September 20th, 2002, 8:30 pm

### Re: C++ quiz - Maths and acccuracy

Is this a rant or does it have a point?

http://yosefk.com/c++fqa/defective.html
With all statements we should not only consider the contents, the author and the context in which the statements are applicable.

Interesting to note that lack of support for modules is not mentioned.
Quite true! Every evaluator of a language views the language through the lens of their own experiences and requirements.

One can never be sure whether a bias for or against a language is self-serving or truthfully derived from the objective properties of the language. The causal arrow is omnidirectional. Cuchulainn
Topic Author
Posts: 60430
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: C++ quiz - Maths and acccuracy

Let $F$ be the set of files (in some format) and the mapping $f, g$ be compression and encrypt operators from $F$ onto $F$, respectively. Is

$f^{2} = f$
$fg = gf$ Posts: 23951
Joined: September 20th, 2002, 8:30 pm

### Re: C++ quiz - Maths and acccuracy

Let $F$ be the set of files (in some format) and the mapping $f, g$ be compression and encrypt operators from $F$ onto $F$, respectively. Is

$f^{2} = f$
$fg = gf$
Neither are true as can be seen with simple counterexamples.

If $f$, for example, analyzes $F$ for duplication of substrings, creates a table of high-frequency substrings, and rewrites $F$ with compact substitutes for the duplicated substrings, then the output will consist of the substitution table and the rewritten files. Running $f$ a second time might find no further compression opportunities in the rewritten strings but it might find them in substitution table that must accompany the rewritten files. Moreover, the substitution table for $f^{2}$ will be different from the substitution table for $f$. Unless $f$ does pre-check to see if $F$ is really $f(F)$, the output of the second $f$ will be different from the first.

For any decent $g$, $fg$ will be a much larger object than $gf$ because $g$ will destroy the repetition of patterns in $F$, creating an output with no structure to compress. (Note: there were some amusing examples of poorly designed $g$ that if you ran them on an image, they would create a file that when displayed as an image showed a recognizable ghost of the original image.) Cuchulainn
Topic Author
Posts: 60430
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: C++ quiz - Maths and acccuracy Posts: 23951
Joined: September 20th, 2002, 8:30 pm

### Re: C++ quiz - Maths and acccuracy

Interesting... It claims to avoid internal overflows.

So what does it return given hypot(0.7*LDBL_MAX.,0.7*LDBL_MAX)? (It should return about 0.98994949*LDBL_MAX) Cuchulainn
Topic Author
Posts: 60430
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: C++ quiz - Maths and acccuracy

Interesting... It claims to avoid internal overflows.

So what does it return given hypot(0.7*LDBL_MAX.,0.7*LDBL_MAX)?  (It should return about 0.98994949*LDBL_MAX)
0.989949493661165 * LDBL_MAX Posts: 23951
Joined: September 20th, 2002, 8:30 pm

### Re: C++ quiz - Maths and acccuracy

Interesting... It claims to avoid internal overflows.

So what does it return given hypot(0.7*LDBL_MAX.,0.7*LDBL_MAX)?  (It should return about 0.98994949*LDBL_MAX)
0.989949493661165 * LDBL_MAX
Nice! (I wonder what trickery they use to compute the squared long double without overflow.) Cuchulainn
Topic Author
Posts: 60430
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: C++ quiz - Maths and acccuracy

Interesting... It claims to avoid internal overflows.

So what does it return given hypot(0.7*LDBL_MAX.,0.7*LDBL_MAX)?  (It should return about 0.98994949*LDBL_MAX)
0.989949493661165 * LDBL_MAX
Nice!  (I wonder what trickery they use to compute the squared long double without overflow.)
Probably cunning stunts.

I only was informed of hypot yesterday. Posts: 23951
Joined: September 20th, 2002, 8:30 pm

### Re: C++ quiz - Maths and acccuracy

0.989949493661165 * LDBL_MAX
Nice!  (I wonder what trickery they use to compute the squared long double without overflow.)
Probably cunning stunts.

I only was informed of hypot yesterday.
That's fine. It's only a tangential development, what? Cuchulainn
Topic Author
Posts: 60430
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: C++ quiz - Maths and acccuracy

Nice!  (I wonder what trickery they use to compute the squared long double without overflow.)
Probably cunning stunts.

I only was informed of hypot yesterday.
That's fine.  It's only a tangential development, what?
BTW what triggered your original question? Where (which language) does it not work? Posts: 23951
Joined: September 20th, 2002, 8:30 pm

### Re: C++ quiz - Maths and acccuracy

Probably cunning stunts.

I only was informed of hypot yesterday.
That's fine.  It's only a tangential development, what?
BTW what triggered your original question? Where (which language) does it not work?
Well if you just use the plain-jane formula, squaring 0.7*LDBL_MAX blows up, no? Cuchulainn
Topic Author
Posts: 60430
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: C++ quiz - Maths and acccuracy

That's fine.  It's only a tangential development, what?
BTW what triggered your original question? Where (which language) does it not work?
Well if you just use the plain-jane formula, squaring 0.7*LDBL_MAX blows up, no?
It gives INFINITY indeed.
In the current case the 'intermediate' value blows up but the final outcome is finite. How do they do it?
// Pity C+11 did not go the whole hog and do a hypot in n-dimensional geometry. Posts: 23951
Joined: September 20th, 2002, 8:30 pm

### Re: C++ quiz - Maths and acccuracy

BTW what triggered your original question? Where (which language) does it not work?
Well if you just use the plain-jane formula, squaring 0.7*LDBL_MAX blows up, no?
It gives INFINITY indeed.
In the current case the 'intermediate' value blows up but the final outcome is finite. How do they do it?
// Pity C+11 did not go the whole hog and do a hypot in n-dimensional geometry.
One way to do would be to normalize the two values by the larger of the two:

c = MAX(ABS(a),ABS(b))

hypot = c*SQRT((a/c)^2 + (b/c)^2)

Of course the above code might fail if both a and b are denormal numbers (https://en.wikipedia.org/wiki/Denormal_number) Cuchulainn
Topic Author
Posts: 60430
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: C++ quiz - Maths and acccuracy

I used complex $x^2 + y^2 = (x + iy)*(x-iy)$ and it worked the option $sqrt(x+iy)*sqrt(x-iy)$. The other one  not. Cuchulainn
Topic Author
Posts: 60430
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: C++ quiz - Maths and acccuracy

Of course, std::hypot is too slow I reckon for graphics/CAD apps (sqrt?).In some case a cubic accuracy Taylor expansion will work.  