Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Math.abs
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
xX_MayDay_Xx




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Mon Mar 20, 2006 8:26 am   Post subject: (No subject)

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




PostPosted: Mon Mar 20, 2006 8:37 am   Post subject: (No 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);
                                                  }
        }
}
Andy




PostPosted: Mon Mar 20, 2006 8:40 am   Post subject: (No subject)

there ya go Smile
xX_MayDay_Xx




PostPosted: Tue Mar 21, 2006 8:32 am   Post subject: (No subject)

ummm....whats a switch application?
Krabjuice




PostPosted: Tue Mar 21, 2006 2:43 pm   Post subject: (No 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.
Dan




PostPosted: Tue Mar 21, 2006 3:13 pm   Post subject: (No 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.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
md




PostPosted: Tue Mar 21, 2006 4:10 pm   Post subject: (No 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:
Sponsor
Sponsor
Sponsor
sponsor
Dan




PostPosted: Tue Mar 21, 2006 6:40 pm   Post subject: (No subject)

I am prity shure that in asm at least seting one byte to 0 with a mov instruction is faster.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
md




PostPosted: Tue Mar 21, 2006 6:47 pm   Post subject: (No 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
rizzix




PostPosted: Wed Mar 22, 2006 3:17 pm   Post subject: (No subject)

1101 & 0111 = 0101
^-- sign bit


I believe that's what Dan was aiming for.
md




PostPosted: Wed Mar 22, 2006 4:21 pm   Post subject: (No 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.
rizzix




PostPosted: Wed Mar 22, 2006 5:39 pm   Post subject: (No 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: