Quantcast

Maximum PC

It is currently Sun Nov 23, 2014 6:56 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: C++ Float check if integer
PostPosted: Thu Feb 14, 2008 2:48 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Tue May 24, 2005 1:19 pm
Posts: 1236
I need a couple lines of code to check if my float variable is an integer or not. I'm working on an assignment to use newton's method to compute the nth root of x and wanted to check if the user input an integer for n or not, but I don't want to use int for n because in my experience inputting something for an int that isn't an integer results in errors and program crashes. Any other ideas are welcome.

Thanks guys!


Top
  Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 3:26 pm 
Team Member Top 100
Team Member Top 100

Joined: Sun Feb 12, 2006 2:52 pm
Posts: 1994
Location: Cryptogram City!
You can input the variable as an integer, then reassign it to your float, or cast it.

Code:
int truncate, i;
float myFloat;
i = 0;
do  {
}while (i (scanf("%d", &truncate)) != 1);

myFloat = truncate;  /* implicit cast to float before assignment */


I haven't ran the above, so it may have errors, but it should give you the idea.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 3:53 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Tue May 24, 2005 1:19 pm
Posts: 1236
Thanks!

Yeah, I kept poking around and I finally came up with a way to truncate as well as tell me if it's been truncated or not so that I can let the user know.

Code:
int number;
double numberdouble;
cout<<"Enter a number: ";
cin>>numberdouble;
number=numberdouble;
if (number!=numberdouble)
{cout<<"Number has been truncated.";}
else
......


Top
  Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 8:54 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Yeah it's easiest to do something like
Code:
bool __inline is_integer(float f){
    int i = f;
    return (f == static_cast<float>(i));
}


Top
  Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 6:01 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Kybo_Ren wrote:
Yeah it's easiest to do something like
Code:
bool __inline is_integer(float f){
    int i = f;
    return (f == static_cast<float>(i));
}

The correctness of you C++ code... really makes me appreciate C. =)


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.