PDA

View Full Version : C++ Does a number have decimal points?



TreeFinger
November 8th, 2007, 06:20 AM
How would I be able to write a statement that could check if a number has decimal places after it? I am trying to find factors of a very large number.

yabbadabbadont
November 8th, 2007, 06:23 AM
If the type of the variable is not a floating point type, then it has no decimals.

dwhitney67
November 8th, 2007, 06:35 AM
Here's a ridiculously stupid way to find out:


#include <iostream>

using namespace std;

int main()
{
double fValue = 5.676;

if ( fValue - (int)fValue > 0.0 )
{
cout << "value has decimal places after it" << endl;
}
else
{
cout << "value does NOT have decimal places after it" << endl;
}
}

TreeFinger
November 8th, 2007, 06:36 AM
It is a long long / long long.

I am trying this, thanks for the tip dwhitney67 .


if ( (BIGNUM % isThisFactor) - static_cast<int>(isThisFactor) > 0 ){

dwhitney67
November 8th, 2007, 06:41 AM
A long long is a 64-bit integer. When you divide (or add, subtract, or multiply) it with another long long (or other integer), you will always end up with an integer type. There will never be values after the "decimal".

As an example,


int ten = 10;
int three = 3;

int result = ten / three;

The result will be 3; not 3.33333333...

yabbadabbadont
November 8th, 2007, 06:41 AM
Never mind.

DavidBell
November 8th, 2007, 06:42 AM
use either

if (A - longlong(A / B) * B == 0)

or

if (A % B == 0) // assuming modulus works for longlong

Rislone
November 8th, 2007, 06:43 AM
Dwhitney

That's the same conclusion I came to also, easiest way at least.

Sporkman
November 8th, 2007, 07:46 PM
if ( fval != floor(fval) )
{
// fval has a fractional component.
}