News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Exponentiation

Started by JK, July 31, 2022, 06:18:09 AM

Previous topic - Next topic

NoCforMe

So where are all those "nans" coming from?
Assembly language programming should be fun. That's why I do it.

jack

Quote from: NoCforMe on July 31, 2022, 01:33:54 PM
I still think you're wrong. Perfectly OK to take the power of a negative number: the sign of the result flips between positive and negative:

-2^2 = 4
-2^3 = -8
-2^4 = 16
-2^5 = -32

and so on.

So far as complex numbers go, I think you're thinking of roots, not powers, as in i = sqrt(-1).

Amiright?
if you did the above in the Windows calculator then like I said, when you enter a number in the calculator and then negate that number then it would be the same as the math expression (-2)^n
try the same with the online calculator https://keisan.casio.com/calculator
this is all I am going to say, you believe what you want, I don't care

jj2007

Quote from: TimoVJL on July 31, 2022, 02:03:29 PM
This might been here earlier, in year 2019 ?

To make it clearer:
#include <stdio.h>
#include <math.h>

int __cdecl main(void) {
  printf("test\n\n\n");
  for (double d = 1.7; d < 2.3; d += 0.1)
printf("-1.23^%1.1f=%3.5f\n", d,pow(-1.23, d));
  return 0;
}


-1.23^1.7=nan
-1.23^1.8=nan
-1.23^1.9=nan
-1.23^2.0=1.51290
-1.23^2.1=nan
-1.23^2.2=nan


Now the same without math.h:
-1.23^1.7=0.00000
-1.23^1.8=-92559641157289296446000000000000000000000000000000000000000000.00000
-1.23^1.9=19035993442894622016370000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000.00000
-1.23^2.0=0.00000
-1.23^2.1=-92559641157289296446000000000000000000000000000000000000000000.00000
-1.23^2.2=-0.00000


Good ol' C always gives you nice surprises :tongue:

jj2007

#18
Quote from: NoCforMe on July 31, 2022, 02:42:16 PM
So where are all those "nans" coming from?

Some CforYou:

#include <stdio.h>
#include <math.h>

int __cdecl main(void) {
  printf("test\n\n\n");
  double result;
  for (double d = 1.7; d < 2.3; d += 0.1) {
_asm int 3;
result=pow(-1.23, d);
_asm nop;
_asm nop;
printf("-1.23^%1.1f=%3.5f\n", d,pow(-1.23, d));
}
  return 0;
}


Somewhere deep in the guts of pow the fpu says "baaaaad nummmber!"

JK

Well, -(2^2) is not the same as (-2)^2 !

The former yields -4, the latter results in 4, so what is the result of -2^2 ?

According to math rules there is an order of math operations, parenthesis having the highest order (= must be calculated first), next is exponentiation and so on (https://en.wikipedia.org/wiki/Order_of_operations). The minus sign used for negation (unary minus) comes after exponentiation. But there are implementations not following this rule by giving negation a higher precedence than exponentiation.

Back to my question: how to get the result of (-2.4)^3.999 (the number "minus 2 point 4" to the power of "3 point 999"). We don´t need complex numbers here btw. 

I refine my previous question: Mathematically (-x)^y is not forbidden, so what must i do in my code, if x is negative? Flip sign, if (x) negative, look at the exponent (y). If the exponent rounded to an integer mod 2 = 0 (that is, an even number), the result is positive, negative otherwise (flip sign afterwards) - is this correct?

JK

jj2007

Quote from: JK on July 31, 2022, 07:11:01 PM
Well, -(2^2) is not the same as (-2)^2 !

Quote from: jj2007 on July 31, 2022, 04:54:23 PMWolfram Alpha, to my knowledge the highest authority for online math, says f*ck the convention, a negative number yields a negative number regardless of the exponent.

-2^2 -4
-(2^2) -4
(-2)^2 +4


:rolleyes:

JK

Sorry jj,

maybe i got this wrong, but to me in x^y, y is the exponent. So "regardless of the exponent" contradicts your sample calculations:

-(2^2)   = - 4
(-2)^2   = + 4


More from Wolfram Alpha:
(-2)^3 = -8
-(2^3) = -8
-(-2)^3 = 8
-(-2)^4 = -16
-(-2^4) = 16
-((-2)^4) = -16


I already know now, what i must know about order of precedence of math operators. So, please could someone (more experienced in math) help me with my coding question ...

Thanks

JK

FORTRANS

Hi,

Quote from: JK on July 31, 2022, 07:11:01 PM
Back to my question: how to get the result of (-2.4)^3.999 (the number "minus 2 point 4" to the power of "3 point 999"). We don´t need complex numbers here btw.

   I disagree, a negative number to a non integer exponent will
normally require complex numbers to be accurate.  Anyway,
here is a FORTRAN program to illustrate.

       PROGRAM MASMEX
C 31 July 2022, silly question from the MASM Forum.

       COMPLEX BASE, EXPO, RESU
       DATA    BASE / (-2.4, 0.0) /, EXPO / (3.999, 0.0) /

       RESU = BASE ** EXPO

       WRITE (*,100) BASE, EXPO, RESU

  100  FORMAT ( ' BASE = ', 2F8.3, ' EXPO = ', 2F8.3,
     1          ' RESU = ', 2F8.3 )
       STOP
       END


   And the output:

BASE =   -2.400   0.000 EXPO =    3.999   0.000 RESU =   33.148  -0.104

   Gack, first such program in seven years.  About the same
number of errors as lines of code.

Cheers,

Steve N.

daydreamer

Quote from: jj2007 on July 31, 2022, 07:11:53 AM
Quote from: daydreamer on July 31, 2022, 06:47:50 AMNewer math library can take advantage of SSE which is easier to perform several math ops simultaneously, instead of your scalar fpu

Interesting. Can you link to one that performs -2.4^4 using SIMD instructions? I'm curious.
I already used x^4 it in cosine taylor series code

.data
align 16
numbers real4 -2.4,-2.3,-2.1,-2.5
result real4 0.0,0.0,0.0,0.0
.code
mov ecx,1000000/4 ;1/4 for packed loop,1/1 for scalar loop
@@L1:
movaps xmm0,numbers
movaps xmm1,xmm0

mulps xmm0,xmm1;x^2
mulps xmm0,xmm1;x^3
mulps xmm0,xmm1;x^4
movaps result,xmm0
sub ecx,1
jne @@L1

my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

jj2007

Quote from: JK on July 31, 2022, 07:11:01 PM
Well, -(2^2) is not the same as (-2)^2 !

The former yields -4, the latter results in 4, so what is the result of -2^2 ?

According to math rules there is an order of math operations, parenthesis having the highest order (= must be calculated first), next is exponentiation

Hi Jürgen,

You are right, and I was wrong. The Windows calculator is somewhat confusing to work with, but I know that's not an excuse ;-)

So: -2.4^3.9999 = -33.1747 because that's -(2.4^3.9999) and does not require complex numbers :cool:

avcaballero

If I'm not wrong
  (-) * (-) = (+)
  (-) * (+) = (-)
  (+) * (-) = (-)
  (+) * (+) = (+)
 
Even or odd is only for integer numbers
 
For any number a, a * a *... * a an even number of times is always positive


If we have  a^b = a * a * ... *a  (b times)

- If "a" is positive, a^b will ve always positive
- If "a" is negative, a^b will be positive only if b is even, negative if b is odd. Hence, if a is negative and b is not integer, we cannot say if a^b will be positive or negative, hence the error message, I guess.


I don't understand why a^b should be understood as complex. Real numbers are actually a subset of complex numbers, where the imaginary part is 0. But a^b will always be real.

c = a + bi, where i = sqrt(-1).

Here we are always dealing with b = 0.

jj2007

Quote from: caballero on August 01, 2022, 02:07:03 AMa^b will always be real

Even with a=-2.4, as in JK's original code, and b=3.99?

avcaballero

I have found this one:

The expression a^(m/n) with fractional exponent is defined for when the nth root of a is a real number.

a^(m/n) = root(n)(a^m)

(-2)^(3/2) is not defined because sqrt(-2) is not a real number.


Only roots with odd index have a result when the radicand is negative

2^3.999 = 2^(4-0.001) = 2^4/(2^0.001), 2^0.001 = 2^(1/1000), 1000 is even.  (understand (-2))

https://ekuatio.com/exponente-fraccionario/

avcaballero

> Even with a=-2.4, as in JK's original code, and b=3.99?

I'm really not sure here, sqrt(-2) is not real, so, if we have to go to complex numbers to get the solution, then I guess the solution cannot be expresed as a real number.

jack

if JK wants to raise both positive and negative reals to arbitrary power then he certainly needs complex math
here is a complex library in cpp as a reference https://github.com/RobTillaart/Arduino/tree/master/libraries/Complex