c - algorithim for calculating the nth root of a number from Wikipedia -
could please tell me what's wrong bit of code below.... i'm trying write program calculate nth root of number output calculation i'm writing keeps coming 0. put numbers in see if result of calculation , assign ans2 0 gets assigned. have ideas?
#define _crt_secure_no_warnings #include <stdio.h> void main(){ int n = 2; double = 4.0, ans = 1.0, ans2 = 1.0, pow = 1.0; ans2 = (1/n)*((n-1)*ans+(a/pow)); printf("\n%lf\n", ans2); printf("\n%lf\n", ans); printf("\n%lf\n", pow); printf("\n%lf\n", a); printf("\n%d\n", n); }
thank you! arp
you have either:
- write
ans2
equationans2 = (1.0/n)*((n-1.0)*ans+(a/pow));
- declare
n
double.
with way have written, compiler interpret 1/n
signed integer. in integer form, 1
just:
00000000000000000000000000000001
so when divide positive integer n > 1
, right shift least significant bit off of end, causing result truncated 0.
Comments
Post a Comment