Author |
Message |
xX_MayDay_Xx
|
Posted: 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? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Andy
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
xX_MayDay_Xx
|
Posted: 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);
}
}
}
|
|
|
|
|
|
![](images/spacer.gif) |
Andy
|
Posted: Mon Mar 20, 2006 8:40 am Post subject: (No subject) |
|
|
there ya go ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
xX_MayDay_Xx
|
Posted: Tue Mar 21, 2006 8:32 am Post subject: (No subject) |
|
|
ummm....whats a switch application? |
|
|
|
|
![](images/spacer.gif) |
Krabjuice
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Dan
![](http://wiki.compsci.ca/images/archive/3/3c/20100325043407!Danspic.gif)
|
Posted: 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! |
|
|
|
![](images/spacer.gif) |
md
![](http://compsci.ca/v3/uploads/user_avatars/1849317514ed6c4399768d.png)
|
Posted: 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:
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Dan
![](http://wiki.compsci.ca/images/archive/3/3c/20100325043407!Danspic.gif)
|
Posted: 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! |
|
|
|
![](images/spacer.gif) |
md
![](http://compsci.ca/v3/uploads/user_avatars/1849317514ed6c4399768d.png)
|
Posted: 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 Wink](http://compsci.ca/v3/images/smiles/icon_wink.gif) |
|
|
|
|
![](images/spacer.gif) |
rizzix
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
md
![](http://compsci.ca/v3/uploads/user_avatars/1849317514ed6c4399768d.png)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
rizzix
|
Posted: 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
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. |
|
|
|
|
![](images/spacer.gif) |
|