473,326 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

how to check for an integer

Hi I've just started c programming and I have a task which requires me to check if a certain value i calculated is an integer. I've tried using
if (fmod(variable,1)==0){
integer..
}else{
not integer..

but its not working for me..
is there any other function that i can use?
and I'm not sure what type of variable should I use(i.e int, double, float) to start with?
Feb 20 '12 #1
2 2086
Banfa
9,065 Expert Mod 8TB
You appear to have been set an impossible task.

You would need to start with a float or double. If you use an int then by definition the value you have is an integer. Here is the issue the float and double types are only approximations to any number, you can never count on them to be exact (particularly after calculations). Since you can convert a double to an integer by casting that means that the equality

d - int(d) == 0

where d is a double (or float) variable

is never true. Even if you expect d to be an integer value it wont be it will be an approximation and d - int(d) will have a small positive or negative result.

The best you can do is test to see if your double is close to an integer value

Expand|Select|Wrap|Line Numbers
  1. const double tolerance = 0.00001;
  2. if (((d - tolerance) < int(d)) && ((d + tolerance) > int(d))
  3. {
  4.   // d is close to an integer
  5. }
  6.  
You can set the tolerance to get an answer to the accuracy you require and I also have an alarm bell going in my head that this algorithm may need to be more complex to deal with negative values of d.
Feb 20 '12 #2
weaknessforcats
9,208 Expert Mod 8TB
I'm not following you. You put the result of your calculation into a variable. So isn't the type of the variable determining the format of the result?

If the variable is a float you cannot convert it to an int without drawing fire from the compiler about possible loss of data (integers have no decimal portion). A typecast just tells the compiler to shut up but the possible loss of data remains.

If the variable is an int, then what it contains is an integer.
Feb 20 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jagdip Singh | last post by:
Hi all, I have four tables create table employee (emp_num integer not null, emp_name varchar(20), emp_commision real, emp_salary real );
7
by: Neil | last post by:
I have a check box on a form that's bound to a function that returns a True/False value. When the user clicks on the check box, I run some code through the MouseDown event. Everything works fine. ...
25
by: junky_fellow | last post by:
Is there any way by which the overflow during addition of two integers may be detected ? eg. suppose we have three unsigned integers, a ,b, c. we are doing a check like if ((a +b) > c) do...
1
by: redpayne | last post by:
Okay, I finally got this program to run according to what the book had us build it as. Now prof wants case 2 and case 3 to prompt again for input, check input to see if it is the correct type, then...
1
by: charles_gero | last post by:
Hi all, I had a question about the topics in the subject and posted to comp.std.c, but feel it may also be appropriate here. Please excuse this crosspost if it is in bad form. I have a...
5
by: pnsreee | last post by:
Hi all, im using following perl code to check integer. use Tie::CheckVariables Tie::CheckVariables->on_error(sub{print "ERROR!"}); tie my $scalar,'Tie::CheckVariables','integer'; #$scalar =...
19
Frinavale
by: Frinavale | last post by:
Filtering user input is extremely important for web programming. If input is left unfiltered users can input malicious code that can cripple your website. This article will explain how to make...
2
by: pashki | last post by:
I have create trigger: CREATE TRIGGER TR NO CASCADE BEFORE INSERT ON T REFERENCING NEW AS newrow FOR EACH ROW MODE DB2SQL WHEN (newrow.id = null) BEGIN ATOMIC set newrow.id = nextval for...
42
by: thomas.mertes | last post by:
Is it possible to use some C or compiler extension to catch integer overflow? The situation is as follows: I use C as target language for compiled Seed7 programs. For integer computions the C...
1
by: thesti | last post by:
hi, i have a field named 'nim', in my table which is a char(10) field. i want to create a constraint that checks the field so the field must be 10 characters long and each character is a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.