Page 2 of 3
Re: Elemental arithmetics; about Sqr(x^2)
Posted: Wed Sep 13, 2023 4:33 pm
by NicTheQuick
Piero wrote: Wed Sep 13, 2023 2:29 pm
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)
Re: Elemental arithmetics; about Sqr(x^2)
Posted: Wed Sep 13, 2023 4:46 pm
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
Re: Elemental arithmetics; about Sqr(x^2)
Posted: Wed Sep 13, 2023 5:18 pm
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
Re: Elemental arithmetics; about Sqr(x^2)
Posted: Wed Sep 13, 2023 5:32 pm
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
Re: Elemental arithmetics; about Sqr(x^2)
Posted: Wed Sep 13, 2023 6:39 pm
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.
Re: Elemental arithmetics; about Sqr(x^2)
Posted: Wed Sep 13, 2023 6:45 pm
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.
Re: Elemental arithmetics; about Sqr(x^2)
Posted: Wed Sep 13, 2023 6:49 pm
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
Re: Elemental arithmetics; about Sqr(x^2)
Posted: Thu Sep 14, 2023 8:35 am
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

Re: Elemental arithmetics; about Sqr(x^2)
Posted: Thu Sep 14, 2023 9:42 am
by NicTheQuick
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.
Re: Elemental arithmetics; about Sqr(x^2)
Posted: Thu Sep 14, 2023 11:39 am
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)
Re: Elemental arithmetics; about Sqr(x^2)
Posted: Thu Sep 14, 2023 12:48 pm
by Psychophanta
Sorry, i referred to @NicTheQuick, and swapped with Little John.
@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)
Re: Elemental arithmetics; about Sqr(x^2)
Posted: Thu Sep 14, 2023 6:55 pm
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.
Re: Elemental arithmetics; about Sqr(x^2)
Posted: Thu Sep 14, 2023 9:53 pm
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
Re: Elemental arithmetics; about Sqr(x^2)
Posted: Fri Sep 15, 2023 6:58 am
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.

Re: Elemental arithmetics; about Sqr(x^2)
Posted: Sat Sep 16, 2023 1:04 pm
by NicTheQuick
For all of you that are interested: I converted the Python fsum function to Purebasic:
viewtopic.php?t=82457