Help with decimal numbers in program in C?

0

How about, I'm doing a project in C, an issue has arisen that I hope the community can help me.

I'm using decimal numbers specifically the data type is Float. When printing my output I see that C is doing a kind of rounding. For example:

The number you should print is .0751632 and print .0800000

The way I print is:

printf("%f\n",aux_no->rutas[y]);

The variable "aux_no" is a structure with the array "routes" as one of its members, that array is of the float type.

Do not know if I should change the data type to Double?

How can I stop rounds that way?

Thanks to everyone for reading.

    
asked by Victor Zepeda 13.04.2016 в 16:07
source

5 answers

2

The array you define within your structure must be type double :

 double rutas[] = {91.023, 0.0751632, 2.03, 3.432, 7.023};

When you print it you can do it as you put it in your question:

for(int y = 0; y<5; y++){
    printf("%f\n",rutas[y]);
}

with that you make sure to print the desired value:

 0.0751632
    
answered by 13.04.2016 в 16:47
1

There seems to be no way to reproduce that effect

#include<stdio.h>

int main(){    

typedef struct aux_no_st{    
    float rutas[10];    
} aux_no_t;

aux_no_t aux_no_var;    
aux_no_var.rutas[1]= 0.0751632;    
aux_no_t *aux_no=&aux_no_var;   
int y=1;    
printf("%f",aux_no->rutas[1]);    
}

I get this result

0.075163

Could you try it and see if it keeps happening?

    
answered by 16.04.2016 в 02:00
0

The behavior in C on the type float and double is the same, it will perform the same rounding.

In the library math.h link You can see the different rounding functions available. Some examples:

#include <math.h>

float val =  0.751632;

float rounded_down = floorf(val * 1000) / 1000;   /* Result: 0.751 */
float nearest = roundf(val * 1000) / 1000;  /* Result: 0.752 */
float rounded_up = ceilf(val * 1000) / 1000;      /* Result: 0.752 */

If this is not enough, you will have to do your own function for that purpose.

    
answered by 13.04.2016 в 16:48
0

The correct format to specify the accuracy used by printf() is:

%f.(n-decimales)

In your case it would be:

printf("%f.7\n",aux_no->rutas[y]);

This would show the 7 decimals of the number you have placed as an example.

But even without specifying the value 7, the default value is 6 decimals so you should get

0.075163

Therefore the error with the rounding or formatting is probably farther back in the assignment to the array.

    
answered by 13.04.2016 в 18:14
0

Rounding is a difficult subject for those who start in this world. What happens is that the precision of the float is low, which indicates that when doing several operations, the error that accumulates increases and leads to results that differ greatly from the actual result. On the other hand, the double variables give us the possibility to cover this problem a little, since the number of bits that are responsible for handling the precision part of the double is almost double that of the float. , therefore, it is, by muuucho, more precise. The conversion specifier associated with the double is% lf.

    
answered by 02.11.2016 в 23:05