Velocity Reviews > Java > How to convert '1' into '-1' and vice versa?

# How to convert '1' into '-1' and vice versa?

-
Guest
Posts: n/a

 06-10-2005
Is there a function to convert '1' into '-1' and vice versa?

Knute Johnson
Guest
Posts: n/a

 06-10-2005
- wrote:

> Is there a function to convert '1' into '-1' and vice versa?

public int negate(int n) {
return -n;
}

or just;

n = -n;

--

Knute Johnson
email s/nospam/knute/

JScoobyCed
Guest
Posts: n/a

 06-10-2005
- wrote:
> Is there a function to convert '1' into '-1' and vice versa?

public class Negate {

private Object lock = new Object();

public static int negate(int n) {
int tmp = 0;
synchronized(lock) {
tmp = n - (2 * n);
int loop = 0;
if(n > 0) {
loop = (new Random()).nextInt(n*1000);
for(int i=0; i<loop; i++) {
int j = i * (-1);
if((0 - j) == n) {
tmp = j;
break;
}
}
}
else {
loop = (new Random()).nextInt((-n)*1000);
for(int i=0; i<loop; i++) {
int j = i;
if((0 - j) == n) {
tmp = j;
break;
}
}
}
}
return tmp;
}

We can still find more complexe

--
JSC

.
Guest
Posts: n/a

 06-10-2005
On Fri, 10 Jun 2005, - wrote:

> Is there a function to convert '1' into '-1' and vice versa?

What datatype is '1' and '-1'? This is not proper notation for Java. The
use of single quotes denotes a char but the '-1' would be an invalid char.

So are you asking to convert "1" to "-1"? Or maybe '1' to "-1"? Or more
likely 1 to -1? The last option is very easy. i.e.

int n = 1;
n = -n;

--
Send e-mail to: darrell dot grainger at utoronto dot ca

Knute Johnson
Guest
Posts: n/a

 06-10-2005
.. wrote:
> On Fri, 10 Jun 2005, - wrote:
>
>
>>Is there a function to convert '1' into '-1' and vice versa?

>
>
> What datatype is '1' and '-1'? This is not proper notation for Java. The
> use of single quotes denotes a char but the '-1' would be an invalid char.
>
> So are you asking to convert "1" to "-1"? Or maybe '1' to "-1"? Or more
> likely 1 to -1? The last option is very easy. i.e.
>
> int n = 1;
> n = -n;
>

..

What is it you are really trying to do?

--

Knute Johnson
email s/nospam/knute/

Roland
Guest
Posts: n/a

 06-10-2005
On 10-6-2005 19:59, Knute Johnson wrote:

> . wrote:
>
>> On Fri, 10 Jun 2005, - wrote:
>>
>>
>>> Is there a function to convert '1' into '-1' and vice versa?

>>
>>
>>
>> What datatype is '1' and '-1'? This is not proper notation for Java. The
>> use of single quotes denotes a char but the '-1' would be an invalid
>> char.
>>
>> So are you asking to convert "1" to "-1"? Or maybe '1' to "-1"? Or more
>> likely 1 to -1? The last option is very easy. i.e.
>>
>> int n = 1;
>> n = -n;
>>

>
> .
>
> What is it you are really trying to do?

I don't think that OP ("-") is the same person you are replying to (".").
--
Regards,

Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \

-
Guest
Posts: n/a

 06-11-2005
.. wrote:
> On Fri, 10 Jun 2005, - wrote:
>
>
>>Is there a function to convert '1' into '-1' and vice versa?

>
>
> What datatype is '1' and '-1'? This is not proper notation for Java. The
> use of single quotes denotes a char but the '-1' would be an invalid char.
>
> So are you asking to convert "1" to "-1"? Or maybe '1' to "-1"? Or more
> likely 1 to -1? The last option is very easy. i.e.
>
> int n = 1;
> n = -n;
>

Indeed, the last option was what I wanted to do.

I was thinking along the line that since there is a Math.abs(double)
function, I thought there was a method to do what I wanted.

Didn't realise it was that easy. No wonder it doesn't warrant a method
of its own.

Wibble
Guest
Posts: n/a

 06-11-2005
JScoobyCed wrote:
> - wrote:
>
>> Is there a function to convert '1' into '-1' and vice versa?

>
>
> public class Negate {
>
> private Object lock = new Object();
>
> public static int negate(int n) {
> int tmp = 0;
> synchronized(lock) {
> tmp = n - (2 * n);
> int loop = 0;
> if(n > 0) {
> loop = (new Random()).nextInt(n*1000);
> for(int i=0; i<loop; i++) {
> int j = i * (-1);
> if((0 - j) == n) {
> tmp = j;
> break;
> }
> }
> }
> else {
> loop = (new Random()).nextInt((-n)*1000);
> for(int i=0; i<loop; i++) {
> int j = i;
> if((0 - j) == n) {
> tmp = j;
> break;
> }
> }
> }
> }
> return tmp;
> }
>
> We can still find more complexe
>
> --
> JSC

I love that! You're my hero!

Antti S. Brax
Guest
Posts: n/a

 06-11-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in comp.lang.java.programmer:
> JScoobyCed wrote:
>> - wrote:
>>> Is there a function to convert '1' into '-1' and vice versa?

>>

<crap removed>
>>
>> We can still find more complexe

> I love that! You're my hero!

That is bullshit. If you're going to write a comedy response
for a trivial question, at least make it work properly.

Hint 1: the code will fail miserably if n is greater than
Integer.MAX_VALUE / 1000.

Hint 2: you can't access instance variables from static
context.

Also, it is trivial to make a complex example by adding
useless code (ie. the unnecessary synchronization block).
You might as well take the source code for Tomcat, embed
it inside your method and then return -n in the end.

If you need a hero in the field of comedy coding, please

--
Antti S. Brax Rullalautailu pitää lapset poissa ladulta
http://www.iki.fi/asb/ http://www.cs.helsinki.fi/u/abrax/hlb/

[1385 messages expunged from folder "Spam"]