Elemental arithmetics; about Sqr(x^2)

For everything that's not in any way related to PureBasic. General chat etc...
User avatar
NicTheQuick
Addict
Addict
Posts: 1519
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

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

Post by NicTheQuick »

Piero wrote: Wed Sep 13, 2023 2:29 pm
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
Yeah, they have completely different datatypes which are also a lot slower than the usual floats, to fix that.
But you also have to look at the precision in general when converting floats to strings. Doubles have a precision of 15 decimal digits and floats have a precision of 7 decimal digits. With a log10() you can automatically choose the maximum precision before converting these numbers into a string:

Code: Select all

Procedure.s StrDmax(d.d)
	Protected e.i = 15 - Round(Log10(d), #PB_Round_Down)
	If e < 0
		e = 0
	EndIf
	ProcedureReturn StrD(d, e)
EndProcedure

Procedure.s StrFmax(f.f)
	Protected e.i = 7 - Round(Log10(f), #PB_Round_Down)
	If e < 0
		e = 0
	EndIf
	ProcedureReturn StrF(f, e)
EndProcedure

Debug StrDmax(10000000000000000)
Debug StrDmax(1.0)
Debug StrDmax(0.1)
Debug StrDmax(0.01)
Debug StrDmax(0.001)

Debug StrFmax(10000000000000000)
Debug StrFmax(1.0)
Debug StrFmax(0.1)
Debug StrFmax(0.01)
Debug StrFmax(0.001)


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

Debug StrDmax(b + c)
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

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

Post by Piero »

NicTheQuick wrote: Wed Sep 13, 2023 4:33 pmyou can automatically choose the maximum precision before converting these numbers into a string
String? ;)

Code: Select all

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

if (a+b) <> 1.2
   Debug "Ooops!"
EndIf
User avatar
NicTheQuick
Addict
Addict
Posts: 1519
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

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

Post by NicTheQuick »

Piero wrote: Wed Sep 13, 2023 4:46 pm
NicTheQuick wrote: Wed Sep 13, 2023 4:33 pmyou can automatically choose the maximum precision before converting these numbers into a string
String? ;)

Code: Select all

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

if (a+b) <> 1.2
   Debug "Ooops!"
EndIf
You never compare the equality of floats like that except you want to check for NaN or infinity. If you want to check for equality then do it like this:

Code: Select all

#epsilon = 1e-10

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

If Abs((a + b) - 1.2) < #epsilon
   Debug "a + b == 1.2"
EndIf
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

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

Post by Piero »

NicTheQuick wrote: Wed Sep 13, 2023 5:18 pm Define a.d = 0.4
Define b.d = 0.8

If Abs((a + b) - 1.2) < #epsilon
Debug "a + b == 1.2"
EndIf
It seems complete nonsense to me (includes the supposed result) so, in case, forgive me...
In some languages you just need to include a math library/function, like, if I remember well, fsum in python
User avatar
NicTheQuick
Addict
Addict
Posts: 1519
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

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

Post by NicTheQuick »

Piero wrote: Wed Sep 13, 2023 5:32 pm
NicTheQuick wrote: Wed Sep 13, 2023 5:18 pm Define a.d = 0.4
Define b.d = 0.8

If Abs((a + b) - 1.2) < #epsilon
Debug "a + b == 1.2"
EndIf
It seems complete nonsense to me (includes the supposed result) so, in case, forgive me...
In some languages you just need to include a math library/function, like, if I remember well, fsum in python
Using Python3 it looks like this and the exact same things are happening too:

Code: Select all

Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 0.4
>>> b = 0.8
>>> a + b
1.2000000000000002
>>> a + b == 1.2
False
If you want to have a library that fixes that "problem" you too have to include a math library/function that solves it for you.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
NicTheQuick
Addict
Addict
Posts: 1519
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

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

Post by NicTheQuick »

By the way: This is also the reason why you never should use floats to make calculations with money. For money use fixed point arithmetic or just change your unit. Instead of 1.20 € you could also store it as 120 ¢ (cents) or even as 120000000µ¢ (micro cents) and then just work with that.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

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

Post by Piero »

NicTheQuick wrote: Wed Sep 13, 2023 6:39 pm If you want to have a library that fixes that "problem" you too have to include a math library/function that solves it for you.
Yeah,
https://medium.com/code-85/how-to-prope ... 9de6b824ce


PS (Stupid AppleScript example):

set a to 0.4 as real
set b to 0.8 as real
a + b

Result: 1.2
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

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

Post by Psychophanta »

STARGÅTE wrote: Mon Sep 11, 2023 9:58 pm Sqr(x^2) = (x^2)^(1/2) = x^(2/2) = x^1 = x is in general not allowed!
Well, returning to the thread issue, and finishing it, for my part at least:
To well math students @STARGÅTE and @NicTheQuick:
an example question:
The well-known Lorentz factor, is correct to express it as: FL = (1-v^2·c^-2)^-1/2 :?:
Last edited by Psychophanta on Thu Sep 14, 2023 12:46 pm, edited 1 time in total.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
NicTheQuick
Addict
Addict
Posts: 1519
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

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

Post by NicTheQuick »

Piero wrote: Wed Sep 13, 2023 6:49 pm
NicTheQuick wrote: Wed Sep 13, 2023 6:39 pm If you want to have a library that fixes that "problem" you too have to include a math library/function that solves it for you.
Yeah,
https://medium.com/code-85/how-to-prope ... 9de6b824ce
Here's the C code for Python's fsum: https://github.com/python/cpython/blob/ ... le.c#L1357

Maybe you can convert it to Purebasic.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
STARGÅTE
Addict
Addict
Posts: 2232
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

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

Post by STARGÅTE »

Psychophanta wrote: Thu Sep 14, 2023 8:35 am
STARGÅTE wrote: Mon Sep 11, 2023 9:58 pm Sqr(x^2) = (x^2)^(1/2) = x^(2/2) = x^1 = x is in general not allowed!
Well, returning to the thread issue, and finishing it, for my part at least:
To well math students @STARGÅTE and @Little John:
an example question:
The well-known Lorentz factor, is correct to express it as: FL = (1-v^2·c^-2)^-1/2 :?:
Yes. As v is always (or must be) smaller than the speed of light c, the radicand is always positive and you can apply the root without restrictions.
But please use parenthesis around the exponent, otherwise it is ^-1 divided by 2 instead of ^(-1/2)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

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

Post by Psychophanta »

Sorry, i referred to @NicTheQuick, and swapped with Little John. :oops:

@STARGÅTE, agree with your answer. For ALL cases, including when c<v (in other universes :lol: ) we can always do this, which is a generalization of the Lorentz factor 8) to make real all universes :
FL = (1-v^2·c^-2) · |1-v^2·c^-2|^(-3/2)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
STARGÅTE
Addict
Addict
Posts: 2232
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

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

Post by STARGÅTE »

Psychophanta wrote: Thu Sep 14, 2023 12:48 pm @STARGÅTE, agree with your answer. For ALL cases, including when c<v (in other universes ) we can always do this, which is a generalization of the Lorentz factor to make real all universes :
FL = (1-v^2·c^-2) · |1-v^2·c^-2|^(-3/2)
No we can't. If v > c the particle is a tachyon with an imaginary mass and also the lorentz factor then just becomes imaginary (and not negativ).

I still don't fully understand the point of this thread. So I'm out now.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Olli
Addict
Addict
Posts: 1240
Joined: Wed May 27, 2020 12:26 pm

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

Post by Olli »

Psychophanta wrote: Mon Sep 11, 2023 8:08 am 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.
I should say however this :

For these two last equalities above, the first is not a one.

Sqr(x^2) = |x| (non math process which can be translated with e(x) = e(f(g(x))) = (e○f○g)(x)
where sqr(x) = (e○f)(x)
as we do not expect a math square root has two opposite (x>0) or conjugated (x<0) results: see below. When √x is only f(x) )


√(x²) = {-x; +x} (math equality)

The first equal is a process operation symbol (a gives b).
sqr(x) <> √x excepted 0 to keep 2 results
but
sqr(x) = |√x|

And the second equal is a math equality symbol (a = b, as b = a)


So, really :
|√(x²)| = x

and

if g(x) = x²
and if f(x) = √x admitting √(-K) with K = 1

then
(f ○ g)(x) = {-x; +x} excepted zero

and if e(x) = |x|

then

Solution1(f○g) being equal to -Solution2(f○g) (minus symbol is too small...)
(e○f○g)(x) = x
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

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

Post by Psychophanta »

STARGÅTE wrote: Thu Sep 14, 2023 6:55 pm If v > c the particle is a tachyon with an imaginary mass and also the lorentz factor then just becomes imaginary (and not negativ).
Ohhh, i didn't know you are another metaphysics and supranatural stuff follower. My condolences. :(
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
NicTheQuick
Addict
Addict
Posts: 1519
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

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

Post by NicTheQuick »

For all of you that are interested: I converted the Python fsum function to Purebasic: viewtopic.php?t=82457
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Post Reply