c++ - Do multiples of Pi to the thousandths have a value that may change how a loop executes? -


recently decided c++, , after going through basics decided build calculator using iostream (just challenge myself). after of complete, came across issue loop exponents. whenever multiple of pi used exponent, looped way many times. fixed in redundant way , i'm hoping might able tell me happened. unfixed code snippet below. ignore above , @ last bit of functioning code. wondering why values of pi throw off loop much. thanks.

bool testfordecimal(double num) /* checks if number given whole or not */ {      if (num > -int_max && num < int_max && num == (int)num) {         return 0;     }     else {         return 1;     } } 

and heres goes wrong (denominator set value of 1)

if (testfordecimal(power) == 1) /* checks if decimal or not */ {     while (testfordecimal(power) == 1) {         power = power * 10;         denominator = denominator * 10;     } } 

if give me explanation great!

to clarify further, while loop kept looping after power became whole number (this happened when power equal multiple of pi such 3.1415 or 6.2830 etc.)

heres complete code can try:

#include <iostream>  bool testfordecimal(double num) /* checks if number given whole or not */ { if (num > -int_max && num < int_max && num == (int)num) {     return 0; } else {     return 1; } }  void foo(double power) { double x = power; if (testfordecimal(x) == 1) /* checks if decimal or not */ {     while (testfordecimal(x) == 1) {         x = x * 10;         std::cout << x << std::endl;      } } }  int main() { foo(3.145); // substitute 3.1415 , doesn't work (this problem) system("pause"); return 0; } 

what's wrong doing this?

#include <cmath> // abs , round #include <cfloat> // dbl_epsilon  bool testfordecimal(double num) {   double diff = abs(round(num) - num);   // true if not whole number   return diff > dbl_epsilon; } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -