# floating point value

• 04-07-2005
twans
floating point value
heyas,

i was wondering if theres a way to determind if a float has a decimal value?

i.e i might have the following times

24:00
09:00
14:30

i want to know if i can iterate through an array or any collection and test if the '.30' exists.

or even more simply a function like isDecimal()

tia

-twans
• 04-07-2005
twans
my appologies,

24.00
09.00
14.30

as im storing them as floats! :P
• 04-07-2005
joshdick
If the number minus the floor of the number is anything other than zero, then there are values stored after the decimal place.
• 04-07-2005
sean
The only thing I can think of, is to subtract .30 from the number, and then use the modulus operator with a 1. If it was n.30, your answer will be 1.

Remember though, that floating point values can never be trusted to be exact, so don't use the == operator to test if it's 1. You could find the absolute value of (1 - yourNumber), and then test to see if that number is less then .1 or something.
• 04-07-2005
twans
works a charm,

those that are interested
Code:

```#include <cmath> floor(float);```

-twan

[edit: silly typo on the include]
• 04-07-2005
joshdick
You're welcome.

The standard library function floor() is in <cmath>. What is <floor>?
• 04-07-2005
twans
can the modulus operator be used on floats?

-thanks for the reply too sean.
• 04-07-2005
TheColonial
Modulus only works on ints I believe.

You could get dirty and cast to int and back to float... something like this:

Code:

```float x = 3.2f; bool hasDecimal = x != (float)((int)x);```
Note: this code hasn't been tested, I just wanted to get the idea across ;)
• 04-07-2005
joshdick
There's an fmod(float x, float y) function in <cmath> that returns the remainder of x/y.
• 04-07-2005
hk_mp5kpdw
Quote:

Originally Posted by twans
can the modulus operator be used on floats?

-thanks for the reply too sean.

Use the fmod function to calculate modulus on floating-point values.

Darn... beaten! :p [/edit]