The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Got a Maths giggle for somebody...

I keep adding coffee, but my brain still won't start... here goes.

If I tap 30 into any calculator and press the +/- key, then it converts 30 to -30 or back again. This allows me to perform calculations on negative numbers.

Is there a PHP function or small script which will take my negative number and convert it to positive or vice versa?

Sorry if this is built in, very silly or I'm missing something fundamental from Maths 101. Coffee is not affecting me today! I'm trying, honestly I am.
Gavin Laking Send private email
Wednesday, September 27, 2006
 
 
How about multiplying by (-1)
Gratzy
Wednesday, September 27, 2006
 
 
Multiply it by -1
James Send private email
Wednesday, September 27, 2006
 
 
Abs?
IanH. Send private email
Wednesday, September 27, 2006
 
 
Doh, missed the vice versa bit. Multiply by -1 instead.
IanH. Send private email
Wednesday, September 27, 2006
 
 
Why not:

$positiv = abs($number);
$negativ = 0 - abs($number);
Marcus Tettmar Send private email
Wednesday, September 27, 2006
 
 
Thanks Marcus. Now I feel like a bigger twit. :-)
Gavin Laking Send private email
Wednesday, September 27, 2006
 
 
Isnt there a unary minus in PHP ? whats the point of substracting anything from zero ?
Bnonymous
Wednesday, September 27, 2006
 
 
<?php

$foo = 50;

echo -$foo;

$bar = -20;

echo -$bar;

echo "done";
?>
Compilenix
Wednesday, September 27, 2006
 
 
How about

number = -number

or

number = -1 * number

(Sorry if the syntax isn't exact--haven't done PHP)
Doug
Wednesday, September 27, 2006
 
 
Wow--when I posted my response, Marcus and Gavin were the only two posts.  I had assumed the responses were sorted by time, but apparently not (since there are now posts between Marcus and Gavin's)
Doug
Wednesday, September 27, 2006
 
 
Those posts were held by FogBugz in the moderation queue. They suddenly appeared because I just approved them.
John Topley Send private email
Wednesday, September 27, 2006
 
 
Thanks everybody. I've made a physical note that when I first detect my brain is not working, I should turn over and go back to sleep.
Gavin Laking Send private email
Wednesday, September 27, 2006
 
 
>> $positiv = abs($number);
>> $negativ = 0 - abs($number);

If I ever saw this code in real life, I think I'd have to pound my head repeatedly into a brick wall in order to try to remove the pain of the thought that someone would actually ever ever do this.
SomeBody Send private email
Wednesday, September 27, 2006
 
 
You're right, he should have done this!

# change sign of number

$positiv = abs($number);
$negativ = 0 - abs($number);
if ($number .gt. 0)
  $number = $negativ;
else
  $number = $positiv;
end if
Meghraj Reddy
Wednesday, September 27, 2006
 
 
Here, I've "optimized" it to be more efficient:

$positi = abs($numbr);
$negati = 0 - abs($numbr);
if ($numbr .gt. 0)
  $numbr = $negati;
else
  $numbr = $positi;
end if
Meghraj Reddy
Wednesday, September 27, 2006
 
 
Multiplying by -1 would toggle the sign.  But if you wanted to store both opposites, and didn't know the sign to start with, that's one way to do it.  Another would be to add logic to multiplying by -1.  If he just wanted to toggle it, not caring what he started with or ended up with, then yes, he should just multiple by -1.
NegPos
Wednesday, September 27, 2006
 
 
Sorry, didn't realise he just wanted to toggle whatever it was.  In that case multiplying by -1 is all that is required.  I made the same mistake as IanH.
Marcus Tettmar Send private email
Wednesday, September 27, 2006
 
 
Meghraj,

My eyes, my eyes.

Which is not to say that I've never seen code like that.

I hope I've never written code like that, at least after my first week programming.
dot for this one
Friday, September 29, 2006
 
 
Don't multiply by -1, I assume PHP, like C, Java, Perl, C++, Python, etc, allows:

newValue = -oldValue

to take a negative.
dot for this one
Friday, September 29, 2006
 
 
Problem with the understanding is that you are calling it (-) a negative as in "-1" is negative 1.

This is called "taking the opposite".
~Eric
Monday, October 02, 2006
 
 
I think Meghraj was kidding..


um..you were, weren't you meghraj? please say yes...
uhhh?
Tuesday, October 03, 2006
 
 
How many programmers does it take to make a number positive/negative?

Apparently, in this thread, dozens.

Wednesday, October 04, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz