Page 1 of 3

Elemental arithmetics; about Sqr(x^2)

Posted: Mon Sep 11, 2023 8:08 am
by Psychophanta
There are bibliographies that say that Sqr(x^2) = |x| , and others that Sqr(x^2) = +-x

It is a discussion that seems to have no end but that some of us see it clearly, that it depends on the strength with which the so-called "hierarchy of operations" is applied.

If you feel like spending time on it, then please reason out the answer about your position in the dilemma.

Code: Select all

v.f=-2.0
r.f=Sqr(Pow(v,2.0))
r2.f=Sqr(v*v)
r3.f=Pow(v,2.0/2.0); <- NOTICE that Sqr(X) is in fact X^(1/2)
debug r
debug r2
debug r3

Re: Elemental arithmetics; about Sqr(x^2)

Posted: Mon Sep 11, 2023 8:27 am
by Lord
Shouldn't it be
r3.f=Pow(v, 1.0/2.0); <- NOTICE that Sqr(X) is in fact X^(1/2)
in order to get the root?
The result ist the correct answer: NaN

Re: Elemental arithmetics; about Sqr(x^2)

Posted: Mon Sep 11, 2023 10:07 am
by juergenkulow
PureBasic does not have complex arithmetic.

Code: Select all

// r2.f=Sqr(v*v)
double p0=(double)((double)v_v*v_v);
double rr2=PB_Sqr_DOUBLE(p0);
wolframalpha sqr(-x^2)

Re: Elemental arithmetics; about Sqr(x^2)

Posted: Mon Sep 11, 2023 9:58 pm
by STARGÅTE
@Psychophanta
What? I don't understand what you want to say.

If you evaluate the root of a number, it gives always the principal root, the positive number.
That's how functions like Sqr() or Pow() are defined in a programming language.
Similar, ASin(0.0) gives only the principal period value of the sine function, although also Sin(2*#Pi) is 0.0.

The option of receiving multiple results is only possible when you solve an equation (not directly possible in Pure Basic). Then, you get a solution space of no solution, one solution or multiple solutions.

Your simplification of Sqr(x^2) = (x^2)^(1/2) = x^(2/2) = x^1 = x is in general not allowed!

Re: Elemental arithmetics; about Sqr(x^2)

Posted: Mon Sep 11, 2023 11:20 pm
by Piero
Also,
Psychophanta wrote: Mon Sep 11, 2023 8:08 am There are bibliographies that say that
i^2 = −1

Re: Elemental arithmetics; about Sqr(x^2)

Posted: Tue Sep 12, 2023 7:51 am
by Psychophanta
@STARGÅTE
The thread is not about program language neither computing. It is just aritmetics. And that is whoy it is posted in 'offtopic' :)
You say
Your simplification of Sqr(x^2) = (x^2)^(1/2) = x^(2/2) = x^1 = x is in general not allowed!
why is it not correct?

@Piero and @juergenkulow, yes, i know, and well? What you say is not to do with this, because this is only for real numbers (sorry i forgot to say it)

@Lord , sorry to say you have not see the dilemma.

Re: Elemental arithmetics; about Sqr(x^2)

Posted: Tue Sep 12, 2023 9:34 am
by STARGÅTE
Psychophanta wrote: Tue Sep 12, 2023 7:51 am @STARGÅTE
The thread is not about program language neither computing. It is just aritmetics. And that is whoy it is posted in 'offtopic' :)
You say
Your simplification of Sqr(x^2) = (x^2)^(1/2) = x^(2/2) = x^1 = x is in general not allowed!
why is it not correct?
I linked the reason.
The rule (x^a)^b = x^(a*b) applies only for integers in a and b, or, if x is positive, for real numbers.

Re: Elemental arithmetics; about Sqr(x^2)

Posted: Tue Sep 12, 2023 1:49 pm
by Psychophanta
STARGÅTE wrote: Tue Sep 12, 2023 9:34 am I linked the reason.
The rule (x^a)^b = x^(a*b) applies only for integers in a and b, or, if x is positive, for real numbers.
Right.
There is a trouble: in arithmetics (bad called 'mathematics') and in nature study (well called 'physics') the exception breaks a rule, so, a rule cannot be validated capriciously for some values ​​and not for others.

About that wiki link and others, we must say there are not in consensus with other bibliographies.
My position is not with rules which are valid only for some specific cases, but a rule should be valid for ALL cases.

How to stay in the real world: x^t = x · |x|^(t-1) 🤩

Re: Elemental arithmetics; about Sqr(x^2)

Posted: Tue Sep 12, 2023 2:28 pm
by STARGÅTE
Psychophanta wrote: Tue Sep 12, 2023 1:49 pm My position is not with rules which are valid only for some specific cases, but a rule should be valid for ALL cases.
This is nonsense. Every rule always has its scope, some times smaller, sometimes larger. There are no rule for all cases.
Even a simple "rule" like x · y = y · x is only valid for real and complex numbers but not for Quaternions, used for 3D-rotations.
In computer science it is even worse, because here the "rule" a · (b + c) = a · b + a · c is not true any more when using floats or doubles:

Code: Select all

Define a.d = 5
Define b.d = 0.4
Define c.d = 0.8

If a*(b+c) = a*b + a*c
	Debug "The world is fine."
Else
	Debug "Opps, "+StrD(a)+"*("+StrD(b)+"+"+StrD(c)+") ≠ "+StrD(a)+"*"+StrD(b)+" + "+StrD(a)+"*"+StrD(c)
	Debug a*(b+c)
	Debug a*b + a*c
EndIf

Re: Elemental arithmetics; about Sqr(x^2)

Posted: Tue Sep 12, 2023 3:22 pm
by Psychophanta
Cmon, the example you have posted is due to the imprecission of the modern computing, and you know it.

In computing you can manage a line with a 0 width, or a plane of 0 width, but only if you previously ideal preparing the algorithms, because 0 width is an ideal used as a tool for real things.

And yes, vectorial product, quat product, etc, don't pass, for example, the conmutative product rule, JUST BECAUSE vector, quaternions, etc, ARE COMPOSED by scalars (real numbers), so they are not real numbers itself.
But, when dealing with real numbers, every serious rule should catch ALL of them, i think this is sense = not notsense :)

Re: Elemental arithmetics; about Sqr(x^2)

Posted: Tue Sep 12, 2023 3:30 pm
by Little John
STARGÅTE wrote: Tue Sep 12, 2023 2:28 pm
Psychophanta wrote: Tue Sep 12, 2023 1:49 pm My position is not with rules which are valid only for some specific cases, but a rule should be valid for ALL cases.
This is nonsense. Every rule always has its scope, some times smaller, sometimes larger. There are no rule for all cases.
Exactly like that!

Re: Elemental arithmetics; about Sqr(x^2)

Posted: Tue Sep 12, 2023 7:00 pm
by Lord
42

Re: Elemental arithmetics; about Sqr(x^2)

Posted: Wed Sep 13, 2023 1:38 pm
by Piero

Code: Select all

Define b.d = 0.4
Define c.d = 0.8

Debug b+c
1.2000000000000001776356839

:shock: :shock: :shock: :x :o :?

Re: Elemental arithmetics; about Sqr(x^2)

Posted: Wed Sep 13, 2023 2:12 pm
by NicTheQuick
Piero wrote: Wed Sep 13, 2023 1:38 pm

Code: Select all

Define b.d = 0.4
Define c.d = 0.8

Debug b+c
1.2000000000000001776356839

:shock: :shock: :shock: :x :o :?
That's not shocking. That's normal in every programming language that uses floats. See https://en.wikipedia.org/wiki/Floating- ... y_problems

Re: Elemental arithmetics; about Sqr(x^2)

Posted: Wed Sep 13, 2023 2:29 pm
by Piero
NicTheQuick wrote: Wed Sep 13, 2023 2:12 pm That's not shocking. That's normal in every programming language that uses floats. See https://en.wikipedia.org/wiki/Floating- ... y_problems
Yeah, I just had forgotten that it happens, being how floats/doubles are "stored"... anyway some languages "fix" that, or have math libraries to avoid the problem