Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

To find absolute value in of floatinng value in C

Status
Not open for further replies.

Joyhtidas

Member level 4
Joined
Nov 30, 2009
Messages
72
Helped
4
Reputation
8
Reaction score
1
Trophy points
1,288
Location
Bangalore, India
Activity points
1,709
Hi...
I working in application which requires the absolute value.
Eg: if value is 1.75 it should give 2
else if value is 1.45, it should return 1 .

I need to know any library function is there in C

Thanks in advance..
 

To suppose it has positive & negative values, and used 'rounding halfway cases away from zero':
If the compiler supports C99 implementation: to use round function.
If no C99 implementation:
Code:
#include <math.h>
int round( float x )
{
   if( x >= 0.0 ) return floor( x+0.5 ); return ceil( x-0.5 );
}
or
Code:
int round( float x )
{
   if( x >= 0.0 ) return (x+0.5); return (x-0.5);
}
 

Do check for abs() function in C. I think that may help you.
Regards,
Jerin. :)
 

Hello!

For your info, what you are looking for is not called absolute value but rounding.

Absolute value of 1.75 is 1.75.
Beside this, abs() as suggested by incomplete_jerin will do exactly this: it will return a float
which is the exact same value for positive number, and its opposite for negative numbers.
But you may consider either to use round() which should be quite standard, or to do it by
hand as suggested by Yager.

Dora.
 

Hi..
Friends,thank you for u'r responses....
I made a function,which is similar to the one that 'yager' suggested...

Jyothidas
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top