Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conversion of negative floating point variable to positive variable

Status
Not open for further replies.

smaksmak

Programmer
Aug 18, 2002
5
IN
Hello friends!

Fisrt of all, I convey my heartful Thanks to all of them subscribed to this wonderful forum.

I would like to have you help me in solving a problem that i face with floating point variable positive/negative value conversion.

Problem scenario:

I declare a variable as float and a negative value is assigned in that by some other operation.later I want that same variable to hold the same value as positive value.hope C compiler has some kind of facility to operate this kind of problem.but i know not.so i seek ur help in this matter.

Example:

Code:
float a,;
float diff;
float ave;

diff=ave - a;

// so now "diff" can store either positive or negative value depends upon the value of "ave" and "a".If it stores positive value,i dun have any problem but if we presume "diff" stores a negative value say "-2.5" then after this above statement execution, I want the "diff" to hold only positive floating point value,'cos i will be using the value of "diff" in some other operation but only with positive value.

say diff = -2.5 then, i want the same variable to hold same value in positive i.e diff = 2.5 .then i will using "diff" in various other operations.

Hope to get ur help soon.
Thanks!

Ragards for helpers,
smaksmak.

 
You could use fabs(double_arg) or fabsf(float_arg)which both return the absolute value of the argument.

/JOlesen
 
Or you could use diff = -diff; which would convert it to a positive number. //Daniel
 
Hello Jolesen! Thanks for replying! I am a novice programmer started practicing in "C".So would you be kind enough to explain ur answer.I really dun get it what does it mean by fabs(double-arg)or fabsf(float_aarg)..what does this command do and where i have to insert and how...please explain to me...Hope u have understand my question,otherwise i have elaborated once again below.Thanks!

Hello Daniel!! Thanks for replying..if we assign
diff = - diff, then we encounter a problem. if the "diff" holds negative value then ur statement would work fine but if it holds positive value then again diff would become negative isnt it..so we cannot use this statement...As I have already stated in the question, I am not going to assign the value for "diff".value will be assigned automatically thru other operation by different variables(statement 1)..so when the execution come to "diff" variable , it may contain any value (positive or negative)..then at the next execution step of "diff" variable(statement 2),I want the "diff" contains only positive value. it is like saying for example
Code:
//statement 1
diff = ave - a 

// so what happens here (statement 1) is,the value of "a" is being subtracted from the value of "ave" and then the result is stored in "diff".Now "diff" can contains any positive or negative value.Now in the next statement(statement 2), I want to make the value of "diff" positive irrespective of whether it contained positve or negative value previously.so if we write a psedocode for the statement below we will write the code like

//statement 2
diff = (positive) diff

// Now i dun know the exact "C" command to fill the gap of "(positive)".Note "diff" ia a floating point variable.

Hope I have explained the problem scenario to the best of my ability.
Thanks!
 
If you only want to convert it if it is negative, then you could use this:
if (diff < 0) /* if diff is negative */
diff = -diff; /* make it positive */ //Daniel
 
Hello smaksmak,

Well really not that much to say about fabsf() !

Here is the documentation from the MSDN help-files :

&quot;fabs returns the absolute value of its argument. There is no error return.&quot;

That's it !!

Part of your original question was :
&quot;later I want that same variable to hold the same value as positive value&quot;

Then you do like this :
diff = fabsf(diff);
With the effect that if diff WAS negative it will now be positive.
If diff was positive it's value won't change.

The 'double_arg' and 'float_arg' was only my poor way of telling that
1 : fabsf() requires a float argument : a float within the ( )
2 : fabs() requires a double argument : a double within the ( )
Sorry if it confused you !

/JOlesen
 
Thanks for ur reply jolesen! it is not working as you said..I am getting a error while compiling the program as &quot; 'fabsf' undefined; assuming extern returning int &quot; and I could not able to execute the program.please let me know if u have any other method.

Thanks for replying daniel! I am not suppose to use control branches(if..then) or conditional loops
(for..while_do..do_while..) in that program.it is a test for me.so please let me know if u can find me any other method to make floating point variable as absolute(positive).
 
Code:
  /* with fabsf function*/
  f = fabsf(f);

  /* without fabsf but with fabs */
  f = (float)fabs((double)f);

  /* without any fabs function */
  f = copysign(f, 1);

  /* without any function nor branch control */
  f = (f > 0) ? f : -f;
  /* This is not branch control, it is an expression ! */
 
Hey smaksmak,

The code will work pretty well if you can build it.

Try to include math.h in your sourcecode :

Put this line in your source-file (put in in top of file .. line 1 maybee ?):

#include <math.h>

/JOlesen
 
Hello friends (Jolesen, Daniel and dchoulette)!!!!

I am very happy yaar!!..Atlast I have found the appropriate statement yet very simple that have got elapsed from our thinking..

I ran the program..it worked perfectly fine..:)))..
the code i have found is..
Code:
diff = diff*diff;
diff = sqrt(diff);
// so if it is negative value then, by the 1st statement the value become positive and by the second statement I get the same value in positive..simple isnt it..OH God! how didnt we think of this very basic school math...

Thanks for you all participating and rendering ur kind suggestions. please feel free to comment on my new code. I Thanks once again to you all dude's.

Regards,
SmakSmak.
 
Hi SmakSmak,

Congratulation !

You really should change it to fabsf()

It probably dosn't matter ... but if/when it comes to efficiency i am sure you have got a one of the more expensive versions ... and now we better stop this discussion ;-)


/JOlesen


 
Thanks for your comment jolesen..I would really try &quot;fabsf&quot; as you said..for now bye.Thanks once again :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top