floating point - Rounding values less than 1 in C? -
i coding graphical program in c , cartesian values in between [-1,1], having trouble rounding off values can use them plotting , further calculations. know how round values greater 1 decimals haven't done before.
so how go rounding values? example,
.7213=.7 .7725= .8 .3666667=.4 .25=.2 or .3 .24=.2
any suggestions gladly appreciated. :)
in languages, people implement such rounding in ad hoc way using *10
, integral rounding, , /10
. example:
$ cat round.c #include <stdio.h> #include <stdint.h> int main() { fprintf(stderr, "%f\n", ((double) ((uint64_t) (10*0.7777))) / 10); return 0; } $ gcc round.c [tommd@vodka test]$ ./a.out 0.700000
Comments
Post a Comment