473,563 Members | 2,856 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

test if integer


Hi,

I have a variable of type double.
I need to know if there is an integer number store there. How can I
test that ?

I also have a default precision for doing this operation.

Many thanks,
Laura

Aug 4 '07 #1
7 30198
laura wrote:
>
Hi,

I have a variable of type double.
I need to know if there is an integer number store there. How can I
test that ?

I also have a default precision for doing this operation.
#include <math.h>

if (fmod(variable, 1) == 0) {/* variable is an integer */}

--
pete
Aug 4 '07 #2
laura wrote:
Hi,

I have a variable of type double.
I need to know if there is an integer number store there. How can I
test that ?

I also have a default precision for doing this operation.
In addition to the following kind of test, think about whether the
ceil() or floor() functions might provide you a test. Consider whether
using them leads to a more reliable test or a more efficient one.

The newer functions round(), trunc(), rint, and nearbyint() might also
provide you with suitable tests. The rint() might be what you want,
since it raises the "inexact" floating point exception if the argument
is not an integer. You might find that learning how to handle floating
point exceptions is not what you want to be doing now, though. If you
do, check your text for a discussion of the <fenv.hheader . If your
implementation does not provide such a header, then don't bother.

#include <stdio.h>
#include <math.h>

int main(void)
{
int i;
double base = 4080., testv, intpart, fraction;
for (i = 2; i <= 20; i++) {
testv = base / i;
printf("Testing %g: ", testv);
fraction = modf(testv, &intpart);
if (fraction) {
printf("fractio nal part = %g\n"
" so probably not an integer.\n"
"integer part = %g.\n\n", fraction, intpart);
}
else {
printf("fractio nal part test == 0,\n" " so an integer.\n"
"integer part = %g.\n\n", intpart);
}
}
return 0;
}

Testing 2040: fractional part test == 0,
so an integer.
integer part = 2040.

Testing 1360: fractional part test == 0,
so an integer.
integer part = 1360.

Testing 1020: fractional part test == 0,
so an integer.
integer part = 1020.

Testing 816: fractional part test == 0,
so an integer.
integer part = 816.

Testing 680: fractional part test == 0,
so an integer.
integer part = 680.

Testing 582.857: fractional part = 0.857143
so probably not an integer.
integer part = 582.

Testing 510: fractional part test == 0,
so an integer.
integer part = 510.

Testing 453.333: fractional part = 0.333333
so probably not an integer.
integer part = 453.

Testing 408: fractional part test == 0,
so an integer.
integer part = 408.

Testing 370.909: fractional part = 0.909091
so probably not an integer.
integer part = 370.

Testing 340: fractional part test == 0,
so an integer.
integer part = 340.

Testing 313.846: fractional part = 0.846154
so probably not an integer.
integer part = 313.

Testing 291.429: fractional part = 0.428571
so probably not an integer.
integer part = 291.

Testing 272: fractional part test == 0,
so an integer.
integer part = 272.

Testing 255: fractional part test == 0,
so an integer.
integer part = 255.

Testing 240: fractional part test == 0,
so an integer.
integer part = 240.

Testing 226.667: fractional part = 0.666667
so probably not an integer.
integer part = 226.

Testing 214.737: fractional part = 0.736842
so probably not an integer.
integer part = 214.

Testing 204: fractional part test == 0,
so an integer.
integer part = 204.

Aug 4 '07 #3
In article <11************ **********@l70g 2000hse.googleg roups.com>,
laura <la************ *@gmail.comwrot e:
>I have a variable of type double.
I need to know if there is an integer number store there. How can I
test that ?
Other posters have given some good ideas.

You do need to be careful, though: if you do fmod(value,1.0) can you be
sure that there will not be any round-off in the last unit?

You also have a problem of resolution: if the input value is (say)
126333081648142 4704.3432 then with a IEEE 754 double, this
would be stored as 1.2633308164814 246e+18 which has been unable
to store the last 104.3432 or so; fmod(1.26333081 64814246e+18,1. 0)
would be 0 even though the input was not an integer as far as
the human was concerned.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Aug 4 '07 #4
On Sat, 04 Aug 2007 18:18:35 +0000, Walter Roberson wrote:
In article <11************ **********@l70g 2000hse.googleg roups.com>,
laura <la************ *@gmail.comwrot e:
>>I have a variable of type double.
I need to know if there is an integer number store there. How can I
test that ?

Other posters have given some good ideas.

You do need to be careful, though: if you do fmod(value,1.0) can you be
sure that there will not be any round-off in the last unit?

You also have a problem of resolution: if the input value is (say)
126333081648142 4704.3432 then with a IEEE 754 double, this
would be stored as 1.2633308164814 246e+18 which has been unable
to store the last 104.3432 or so; fmod(1.26333081 64814246e+18,1. 0)
would be 0 even though the input was not an integer as far as
the human was concerned.
What? She asked how to know "if there is an integer number
store[d] there". She did not ask to know whether it is an integer
because of rounding, considering there is no way a C program can
determine that.

--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 5 '07 #5
In article <pa************ *************** *@NOSPAM.it>,
Army1987 <ar******@NOSPA M.itwrote:
>On Sat, 04 Aug 2007 18:18:35 +0000, Walter Roberson wrote:
>In article <11************ **********@l70g 2000hse.googleg roups.com>,
laura <la************ *@gmail.comwrot e:
>>>I have a variable of type double.
I need to know if there is an integer number store there. How can I
test that ?
>You also have a problem of resolution: if the input value is (say)
12633308164814 24704.3432 then with a IEEE 754 double, this
would be stored as 1.2633308164814 246e+18 which has been unable
to store the last 104.3432 or so;
>What? She asked how to know "if there is an integer number
store[d] there". She did not ask to know whether it is an integer
because of rounding, considering there is no way a C program can
determine that.
But she asked whether there is an integer "number" stored there,
not an integer "value". "number" is the pure concept, with "double"
being an approximation. When one examines the approximation, one cannot
be sure that the original -number- was an integer or not.
--
Programming is what happens while you're busy making other plans.
Aug 5 '07 #6
On Sun, 05 Aug 2007 19:51:55 +0000, Walter Roberson wrote:
In article <pa************ *************** *@NOSPAM.it>,
Army1987 <ar******@NOSPA M.itwrote:
>>On Sat, 04 Aug 2007 18:18:35 +0000, Walter Roberson wrote:
>>In article <11************ **********@l70g 2000hse.googleg roups.com>,
laura <la************ *@gmail.comwrot e:
I have a variable of type double.
I need to know if there is an integer number store there. How can I
test that ?
>>You also have a problem of resolution: if the input value is (say)
1263330816481 424704.3432 then with a IEEE 754 double, this
would be stored as 1.2633308164814 246e+18 which has been unable
to store the last 104.3432 or so;
>>What? She asked how to know "if there is an integer number
store[d] there". She did not ask to know whether it is an integer
because of rounding, considering there is no way a C program can
determine that.

But she asked whether there is an integer "number" stored there,
not an integer "value". "number" is the pure concept, with "double"
being an approximation. When one examines the approximation, one cannot
be sure that the original -number- was an integer or not.
If I write
double a;
a = 1.0000000000000 000000000000000 000000000000000 000000000000000 1;
supposing DBL_EPSILON is large enough, that number gets rounded
*before* being stored in a. The number stored in a is 1.
If she meant "How do I know whether the number 'intended to' be
stored is integer", well, short of having the program read its own
source for constants, tricky workarounds for strtod, and
calculations with infinite precision for other things such as
(1.0 / 3.0) * 3.0, how the hell is she supposed to do that?

--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 6 '07 #7
In article <pa************ *************** *@NOSPAM.it>,
Army1987 <ar******@NOSPA M.itwrote:
>On Sun, 05 Aug 2007 19:51:55 +0000, Walter Roberson wrote:
>In article <pa************ *************** *@NOSPAM.it>,
Army1987 <ar******@NOSPA M.itwrote:
>>>On Sat, 04 Aug 2007 18:18:35 +0000, Walter Roberson wrote:
>>>In article <11************ **********@l70g 2000hse.googleg roups.com>,
laura <la************ *@gmail.comwrot e:
>I have a variable of type double.
>I need to know if there is an integer number store there. How can I
>test that ?
>>>You also have a problem of resolution: if the input value is (say)
126333081648 1424704.3432 then with a IEEE 754 double, this
would be stored as 1.2633308164814 246e+18 which has been unable
to store the last 104.3432 or so;
>If she meant "How do I know whether the number 'intended to' be
stored is integer", well, short of having the program read its own
source for constants, tricky workarounds for strtod, and
calculations with infinite precision for other things such as
(1.0 / 3.0) * 3.0, how the hell is she supposed to do that?
Exactly. She can't. There's the problem of resolution; infinite
resolution, just as you point out.

When people ask questions here, what they ask to do isn't always
possible to do. But often, what they ask to do isn't actually
what they want to do; sometimes it is more feasible and sometimes
it is even less feasible; we can but point out the issues and hope
that helps.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Aug 6 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
14051
by: NotGiven | last post by:
I need to check the $_GET to make certain it is a positive integer. is_integer($_GET) is not working. I think it thinks it's a sting. So I tried casting it to an int using, is_integer((int)$_GET). Then whatever I threw in the _GET variable passed as an integer, even strings. Any ideas or links or functions are much appreciated!
467
21277
by: mike420 | last post by:
THE GOOD: 1. pickle 2. simplicity and uniformity 3. big library (bigger would be even better) THE BAD:
7
17191
by: Jeff Gilchrist | last post by:
I have tried searching the newsgroup along with the GCC site and could not find what I think is probably a simple solution. I am using a 64bit unsigned long long integer and can manipulate 64bits within the variable but I cannot assign a 64bit constant to it. With my simple test program: int main() {
2
425
by: js | last post by:
I need to test if an integer variable is null or not. I tried to do int myVairable; myVariable = Int32.Parse(txtControl1.Text.Trim()); if (myVariable != null) {// do something} C# does not allow this statement. How do I test an integer to see if it is null or not? Thanks.
35
2541
by: Cor | last post by:
Hallo, I have promised Jay B yesterday to do some tests. The subject was a string evaluation that Jon had send in. Jay B was in doubt what was better because there was a discussion in the C# newsgroup on 25 September. The regular expressions where in that newsgroup too involved. I told yesterday night, to Jay that I would test all 4...
2
1759
by: Greg | last post by:
Please note: I have cross posted this from Newsgroup: microsoft.public.dotnet.framework.aspnet.webservices with a few minor changes... I am having a simple problem setting up the security on my test web service... My Web service code is: Imports System.Web.Services
20
34703
by: Jay | last post by:
Hello, I am sure this has a quick and easy solution but I can't find it. I need to store 19 '9's in an integer which means i need an unsigned 64 bit integer. in Visual Studio the code would be: __int64 test = 9999999999999999999;
43
8398
by: Steven T. Hatton | last post by:
http://public.research.att.com/~bs/bs_faq2.html#int-to-string Is there no C library function that will take an int and convert it to its ascii representation? The example Bjarne shows in his faq is not extremely convenient. -- NOUN:1. Money or property bequeathed to another by will. 2. Something handed down from an ancestor or a...
8
2051
by: aarklon | last post by:
hi all, in http://www.c-faq.com/cpp/ifendian.html it is said that integer formats used in pre processor #if expressions are not the same as those will be used at run time. can any one explain about this difference in formats???
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.