C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 17th, 2014, 03:01 AM
fleazo fleazo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2014
Posts: 1 fleazo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 45 m 48 sec
Reputation Power: 0
Simple question about my first C program: why isn't if(5.00000 == 5) coming as true?

I need to do the following:

take an int n, and an int m, and calculate the mth root of n (which I can do in C as pow(n,(double)1/m)

I then need to find out if the result is an integer.

I found that a suitable way in C to do this is:

double k = pow(n,(double)1/m);

int truncated = (int)k;
if( truncated == k ) { // the result was an integer; }

This seemed to work for awhile. It worked for example, when n = 16, and m = 2, in that case I got:

16^(0.5) = 4.00000
I then got truncated = 4, and the if statement was true.

However, as the number n got larger I noticed it isn't working. Best example being n = 3125 and m = 5. In this case I get: 3125^(1/5) = 5.0000000. But then for some reason the if statement fails.

I was curious why this happens?


EDIT:

Here is sample code you can use to test this out:


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

int main() {
double k = 0;
int truncated = 0;

// works on a small number
k = pow(16,(double)1/2);
truncated = (int)k;
printf("16 to the one half power is %f\n",k);
if( truncated == k ) {
printf("\tThis produced an integer\n");
}

// doesn't work on a larger number
k = pow(3125,(double)1/5);
truncated = (int)k;
printf("3125 to the one fifth power is %f\n",k);
if( truncated == k ) {
printf("\tThis produced an integer");
}

return 0;
}



This produces the output:

16 to the one half power is 4.000000
This produced an integer
3125 to the one fifth power is 5.000000


Furthermore, is there a better way also of testing if something is an integer?

Reply With Quote
  #2  
Old February 19th, 2015, 12:55 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
http://floating-point-gui.de/
__________________
There is no such thing as C/C++, you either program C or C++

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Simple question about my first C program: why isn't if(5.00000 == 5) coming as true?


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap