python - Windows 7 cmd, from Visual Studio, not displaying floats -


in visual studio 2015, made program calculate pi. variable pi float (i wasn't sure if necessary thought might in case), anyway.. here's function:

float pi(int accuracy) {     float pi = 3;     int nint = 2;     int next;     (int = 0; < accuracy; i++) {         next = 4 / (nint * (nint + 1) * (nint + 2));         nint += 2;         if (i % 2 == 0) {             pi += next;         }         else {             pi -= next;         }     }     return pi; } 

so far can see, should return float variable. when call it, use 2000 parameter, , should return quite accurate representation of pi. visual studio displays cmd, instead of writing decimal places, shows 3.
decided test in python. here's code:

pi = 3 n_int = 2  # i'm using 1000 accuracy, because python's slower. in range(1000):     nextint = 4 / (n_int * (n_int + 1) * (n_int + 2))     n_int += 2     if % 2 == 0:         pi += nextint     else:         pi -= nextint  print(pi) input() 

i tested python because way there 2 different ways of outputting number. first, ran directly idle, displayed output in idle window. displayed 3.141592653340544 (this isn't entirely accurate, expected). when double-click python file in windows explorer (making open in cmd), , displays 3, c++ example.
can safely it's problem cmd, perhaps i've changed setting or might make happen (although can't remember doing this).
so, how, in cmd, can display float or long number?

next = 4 / (nint * (nint + 1) * (nint + 2));

(nint * (nint + 1) * (nint + 2)) int. 4 int. dividing 2 ints gives int, removing decimal part (because ints have no decimal parts).

you should change 4.0 or 4.0f use floating-point division.

and should make next float or double, because number needs store isn't whole number.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -