Computer Science Canada

Math.abs

Author:  xX_MayDay_Xx [ Mon Mar 20, 2006 8:25 am ]
Post subject:  Math.abs

ok....i know that Math.abs gives the absolute value of a number..ex(the absolute value of -12...is..12) put i need to know how to do this without using the Math.abs application....i think it has something to do with multipling the number by it's self then dividing by 2?

Author:  Andy [ Mon Mar 20, 2006 8:26 am ]
Post subject: 

how about give us the definition of absolute value, once you understand what it means, this question becomes intuitive

Author:  xX_MayDay_Xx [ Mon Mar 20, 2006 8:37 am ]
Post subject: 

yeah.....i was an idiot......basic rule of math....anything *1 equals its self...that combined with the rules of integers -*+=-....or -*-=+...thats all had to do....i was an idiot...i will no freely except harassment for the uber n()()b...
code:
public class AbsoluteValue
{
        public static void main(String[]args)
        {
                int num;
                System.out.println("please enter a number");
                num=In.getInt ();
                if (num>=0)
                {
                System.out.println("the absolute  value of that number is..."
                                   + num);
            }
            else
            {
                        System.out.println("the absolute value of "+num+" is..."
                                          +num*-1);
                                                  }
        }
}

Author:  Andy [ Mon Mar 20, 2006 8:40 am ]
Post subject: 

there ya go Smile

Author:  xX_MayDay_Xx [ Tue Mar 21, 2006 8:32 am ]
Post subject: 

ummm....whats a switch application?

Author:  Krabjuice [ Tue Mar 21, 2006 2:43 pm ]
Post subject: 

Something that uses the switch statement, i'd assume. Switch is a "in the case of" type of control sequence. People generally use if's, but switch has it's uses.

Author:  Dan [ Tue Mar 21, 2006 3:13 pm ]
Post subject: 

I think the most effsent way of doing this whould be to set the singed bit to 0 or what ever means postive on your cpu.

Tho this whould probly be a bit complicated for most peoleop and is not real high level progaming but rather low level asm type stuff. I guse this is what Math.abs is for =p.

Author:  md [ Tue Mar 21, 2006 4:10 pm ]
Post subject: 

I dunno dan; getting a single bit isn't easy; I think a strait compare might be faster

n is $2
$3 is temporary storage

compare:
mips:

bgtz $2, ret
addi $3, $0, -1
mult $2, $3
mflo $2
ret:


bit-wise
mips:

addi $3, $3, 0
sra $3, $3, 31
bne $3, $0, ret
addi $3, $0, -1
mult $2, $3
mflo $2
ret:

Author:  Dan [ Tue Mar 21, 2006 6:40 pm ]
Post subject: 

I am prity shure that in asm at least seting one byte to 0 with a mov instruction is faster.

Author:  md [ Tue Mar 21, 2006 6:47 pm ]
Post subject: 

Wha? I'm using words... and I don't set any to zero..

setting the sign bit to 0 instead of 1 would not change the sign of the number at all, it would radically change the number though.

1110 + 0010 = 0000. 0010 is the positive number, 1110 is it's negative. 0110 + 0010 = 1000, clearly not zero Wink

Author:  rizzix [ Wed Mar 22, 2006 3:17 pm ]
Post subject: 

1101 & 0111 = 0101
^-- sign bit


I believe that's what Dan was aiming for.

Author:  md [ Wed Mar 22, 2006 4:21 pm ]
Post subject: 

yeah, but just checking it is still slow. It's best case two instructions to compare just hte bit, whereas a simple less then is only one.

Author:  rizzix [ Wed Mar 22, 2006 5:39 pm ]
Post subject: 

1101 & 0111 = 0101
^-- sign bit / resut is a positive number: 0xxx

0110 & 0111 = 0110
^-- sign bit / result is a positive number 0xxx

Smile It is fast. But as we discussed in #compsci.ca it isin't actually the case. Two's complement is a whole lot trickier than just fixing one bit.


: