banner



How To Write Answer From Negative To Positive In Mathematica

#1

  • D.I.C Head

Reputation: 0

  • View blog
  • Posts: 135
  • Joined: 05-April 08

convert to positive or negative value

Post icon  Posted 02 May 2008 - 12:30 AM

in C let say i = a - b and i evaluates to a negative value -4, how do i convert it to positive value become 4 and vice versa? thanks in advance.

Is This A Good Question/Topic? 0

  • +

#2 gabehabe User is offline

Reputation: 1438

  • View blog
  • Posts: 11,017
  • Joined: 06-February 08

Re: convert to positive or negative value

Posted 02 May 2008 - 01:48 AM

Why not just multiply it my -1? But, for making it positive, it becomes a little more annoying - it doesn't seem to work to just multiply it by 1. Take this example:

#include <iostream>  using namespace std;  int main () {     int x = 5; // start as 5     cout << x << endl;     x *= -1; // make the number negative     cout << x << endl;     x -= x*2; // make it positive     cout << x << endl;      system("pause");     return 0; }            

Hope this helps :)

#3 Guest_Whizzy*

Re: convert to positive or negative value

Posted 02 May 2008 - 05:10 AM

ABS...

This function gives you the absolute value of your number... -4 becomes 4.

The reverse is also true... just throw a "-" sign infront of abs.
as in the following...

#include <cstdlib> #include <iostream> #include <stdio.h> #include <stdlib.h>  int main(int argc, char *argv[]) {   int a,b;   a=4; 	b=abs(a); 	printf ("b=%d\n",B);       //  Now, a negative      b=-abs(a);   printf ("b=%d\n",B);    system("PAUSE"); 	return EXIT_SUCCESS; }            

This post has been edited by Whizzy: 02 May 2008 - 05:35 AM

Was This Post Helpful? 0

#4 kckc314 User is offline

  • D.I.C Head

Reputation: 0

  • View blog
  • Posts: 135
  • Joined: 05-April 08

Re: convert to positive or negative value

Posted 02 May 2008 - 09:33 PM

View PostWhizzy, on 2 May, 2008 - 05:10 AM, said:

ABS...

This function gives you the absolute value of your number... -4 becomes 4.

The reverse is also true... just throw a "-" sign infront of abs.
as in the following...

#include <cstdlib> #include <iostream> #include <stdio.h> #include <stdlib.h>  int main(int argc, char *argv[]) {   int a,b;   a=4; 	b=abs(a); 	printf ("b=%d\n",B);       //  Now, a negative      b=-abs(a);   printf ("b=%d\n",B);    system("PAUSE"); 	return EXIT_SUCCESS; }              

hang on let me clarify with this:

the first part should be written as if a=-4; if i want to convert it to become 4(positive) then 	b=abs(a); 	printf ("b=%d\n",B);  if a=4; if i want to convert it to become -4(negative) then 	b=-abs(a); 	printf ("b=%d\n",B);            

correct me if i am wrong?

#5 Guest_Whizzy*

Re: convert to positive or negative value

Posted 02 May 2008 - 09:38 PM

I made one mistake....
printf ("b=%d\n", B ) ; would become
printf ("b=%d\n", b ) ; with a lowercase b or you'll get an undeclared error.

Quote

hang on let me clarify with this:

the first part should be written as if a=-4; if i want to convert it to become 4(positive) then 	b=abs(a); 	printf ("b=%d\n",B);  if a=4; if i want to convert it to become -4(negative) then 	b=-abs(a); 	printf ("b=%d\n",B);              

correct me if i am wrong?

Actually, If statements require { not ;
So it would be

if (a=4){ b=-abs(a); }  and  if (a=-4){ b=abs(a); }            

But yes, you have the idea...

This post has been edited by Whizzy: 02 May 2008 - 09:53 PM

Was This Post Helpful? 0

How To Write Answer From Negative To Positive In Mathematica

Source: https://www.dreamincode.net/forums/topic/50804-convert-to-positive-or-negative-value/

Posted by: robertsfromens.blogspot.com

0 Response to "How To Write Answer From Negative To Positive In Mathematica"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel