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

Discussion in 'Java' started by -, Jun 10, 2005.

1. ### -Guest

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

-, Jun 10, 2005

2. ### Knute JohnsonGuest

- 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/

Knute Johnson, Jun 10, 2005

3. ### JScoobyCedGuest

- 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 :-D

--
JSC

JScoobyCed, Jun 10, 2005
4. ### .Guest

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

., Jun 10, 2005
5. ### Knute JohnsonGuest

.. 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/

Knute Johnson, Jun 10, 2005
6. ### RolandGuest

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_/ /__/
/ \ /_/ / \

Roland, Jun 10, 2005
7. ### -Guest

.. 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.

-, Jun 11, 2005
8. ### WibbleGuest

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 :-D
>
> --
> JSC

I love that! You're my hero!

Wibble, Jun 11, 2005
9. ### Antti S. BraxGuest

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 :-D

> 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
read this: http://home.tiac.net/~cri/2001/badsort.html

--
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"]

Antti S. Brax, Jun 11, 2005

### Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.