News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

FPU compare real to integer problem

Started by RuiLoureiro, March 17, 2012, 01:36:34 PM

Previous topic - Next topic

raymond

I tried a long expression for X^Y where X would be negative and Y not an integer and it returned ERROR.

Now, I tried Dave's attempt of -3^2.5 and it doesn't return ERROR but a "wrong" result.????????????
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

RuiLoureiro

jj,
        Thanks  :wink

dave,
        Thanks.  :wink
        It doesnt exit. Close the window !

raymond,
        Thanks  :wink
       
Quote
I tried a long expression for X^Y where X would be negative and Y not an integer and
it returned ERROR.
If X<0  and Y is not an integer it may be an ERROR or the prog
            doesnt solve it correctly. Please tell me the case.
             

            I didnt solve all cases. I have a lot of work to do in
            the next days.

Quote
Now, I tried Dave's attempt of -3^2.5 and it doesn't return ERROR
but a "wrong" result.????????????

        "wrong" result ? But as dave show, -3^2.5=-15.5884572681 is correct.
        I dont know what case you are talking about.

dioxin

QuoteBut as dave show, -3^2.5=-15.5884572681 is correct.
No it isn't.
-3^2.5 = -3^2 * -3^0.5
       =   9  * ±1.7320508j
       = ±15.58845j

There are 2 solutions, both are imaginary.

Paul.

RuiLoureiro

Quote from: dioxin on March 20, 2012, 09:33:56 PM
QuoteBut as dave show, -3^2.5=-15.5884572681 is correct.
No it isn't.
-3^2.5 = -3^2 * -3^0.5
       =   9  * ±1.7320508j
       = ±15.58845j

There are 2 solutions, both are imaginary.

Paul.

dioxin,
        Where did you learn that ?
       
        As far as i know
       
        -3^2.5 means  this: -(3^2.5) so make what you want (3^2 * 3^0.5)

        If you want to raise (-3) to (2.5) write (-3)^2.5

        What do you mean if you write this: +9-3^2 ?

dioxin

Rui,
   a unary minus sign is the highest priority mathematical operator. It's not really an operator at all but an intrinsic part of the number, to be applied before any other operator is considered.
   

   -3^2 means (-3)^2 not -(3^2)
   -3 is not 3 subtracted from zero, it is a single entity "minus3", just as 12 is not two separate digits but a single number "twelve". You can't remove the - from the 3 without changing its meaning any more than you can remove the 2 from the 1 and still leave it as 12.
   Neither can you convert the unary - to a binary operation by prefixing a zero which should not be there and then redo the calculation (which appears to be what you're doing):
   0-3^2 = -9
        as that 0 changes the meaning of the calculation.
   

   Consider 2 * -3.
   Do you not expect the result to be -6?
   Or do you place a zero in the unoccupied position ahead of the minus sign to convert it to a binary operation and end up with
   2*0-3 = -3

   Excel gives -3^2 = 9
   My 2 pocket calculators give -3^2 = 9
   From what I was taught at school -3^2 = 9
   Microsoft PowerToy calculator gives -3^2 = 9
   
   Perhaps some programming languages have it wrong. If so then it's a good job they have () to circumvent the error.

Paul.

raymond

My bad. I realized my error while having dinner.

-3^2.5 = -15.5884572681 is entirely correct because the "^" takes precedence over the "-" sign in such an expression. The "-" must thus be used only after the power is applied. It is thus equal to -(3^2.5).

(-3)^2.5 correctly results in ERROR.

My apology again for my ?????. Your prog was correct.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

raymond

Paul,
Your calculator gives you -3^2=9 because you effectively put backets around the -3 before applying the power, (-3)2'

Think of the expression 4*5-3^2.
You would first apply the "*" operator, then the "^" operator, and apply the "-" operator as the last one, certainly not before the "^" operator unless you use brackets such as (4*5-3)^2.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

dioxin

Raymond,
no implied brackets anywhere. The calculator takes the full algebraic expression and then calculates the result when I press the = key so it is judging precedence of the operators with unary - ahead of ^.
Perhaps it's an age thing. My main calculator is over 30 years old. Maths was done properly in those days!

Your second case is different because the - is a binary operator there.

In a case such as -3^2 it's clearly unary and is part of the number minus3.
With 4*5-3^2 it's clearly binary otherwise there is no operator between the 4*5 and the -3^2
You could have 4*5- -3^2 where the - acts as binary the first occurence and unary the second occurence.

It's not so obvious with something like:
x=-3
-x^2 = ?
In this case the - is not binary but neither is it clearly part of the number x. It's actually a negation operator on what follows but it makes sense to me, and it's how I was taught, that the unary minus is the highest precedence operator so, even in this case the - should be applied first giving -x^2 = 9 when x=-3.

There are differences of opinion out there. Programming languages C and BC, Sharp Corporation of Japan (the makers of my calculator), Microsoft, and My various maths teachers all put unary - right at the top in precedence. It seems more logical to do it that way otherwise the whole range of negative numbers are not representable except as a negation of a positive number and there's no reason to arbitrarily relegate half of all real numbers to be functions of the other half in that way. Maths is comlex enough without that sort of thing.

Paul.

qWord

Quote from: dioxin on March 21, 2012, 12:33:49 AMit's how I was taught, that the unary minus is the highest precedence operator so, even in this case the - should be applied first giving -x^2 = 9 when x=-3.
that assumption is false!
-x^2 = -1*x^2 = -1*(-3)^2 = -9

try this one: http://www.wolframalpha.com
FPU in a trice: SmplMath
It's that simple!

dedndave

Rui's program is correct
i was thinking -3^2.5 was complex   :P
his program does return an error for (-3)^2.5
and -3^2.5 should be interpreted the same as -(3^2.5)

http://en.wikipedia.org/wiki/Order_of_operations

you'd think we all would know that, by now   :bg

raymond

QuoteExcel gives -3^2 = 9

I use QuattroPro, Corel's version of Excel. When I enter -3^2, it displays -9 as the answer. Only when I enter (-3)^2 do I get 9 as the answer.

Entering -3^2.5, I get -15.58846 but entering (-3)^2.5, it results in ERROR.

I wonder how come those two similar programs would produce different results with the same input.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

dedndave

ok - gotcha
Excel seems to be messed up

raymond

OpenOffice also returns 9 for -3^2.
It also returns an error for -3^2.5!!!! :dazzled:
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

dioxin

qWord,
not false at all. Given there are 2 ways to interpret it, I'd suggest that my way makes more sense.

To do it your way, the - in -2 becomes a negation operator on the number 2. It's then only possible to represent that negative number in terms of a function on the positive number. That makes no sense as there is no fundamental reason for negative numbers to be derived in this way. It's far more sensible to have -2 directly represent the required number in which case the - is not an operator but a part of the number.

If the - in -2 is to be treated this way then it follows that the - in -x ought to be treated the same otherwise we don't now have 2 uses of -, we have 3, one is a representation of a negative number, one as a binary subtraction operator and a one as a unary negation operator.

There is no reason to assume that -x is really -1 * x, that's fiddling the facts to match the desired outcome which just adds unnecessary complexity. You start adding a 1 after a - if it precedes a variable but not if it precedes a literal? You then must assume implied multiplication when programming languages never use implied multiplication. You then add 0 ahead of a - if there is no other number ahead of it (as in -x^2 should be treated as 0-x^2 where the unary - is changed to a binary -)?

Why do these fiddles when treating - as the highest priority removes the need for any fiddling as well as reducing complexity and making complete sense?



dedndave,
a quote (carefully framed to make a point!) from the Wikipedia page you refer to:   
Quoteunary operators have a higher priority than binary operators, that is, the unary minus (negation) has higher precedence than exponentiation



Raymond,
   OpenOffice Calc follows the Excel rules and gives -3^2 = 9




dedndave,
QuoteExcel seems to be messed up
no, you have it wrong. Excel got it right. Open Office agrees as do C, BC, Sharp, Mr.Duggan (my maths teacher) and others.

Paul.

qWord

Quote from: dioxin on March 21, 2012, 01:55:20 AM
Why do these fiddles when treating - as the highest priority removes the need for any fiddling as well as reducing complexity and making complete sense?
-3^2 = (-3)*(-3) = 3^2
-3^2 = 3^2 ???
make this sense?

FPU in a trice: SmplMath
It's that simple!