Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

What is the difference between float and double in C

1 Answer

0 votes
// double has 2x the precision than float

#include <stdio.h> 
#include <math.h> 

void float_calc(float a) { 
    float n = a / 98;
    float sum = 0;

    for (int i = 0; i < 817; i++)
            sum += n;

    printf("%.7g\n", sum);   

} 
  
void double_calc(double a) { 
    double n = a / 98;
    double sum = 0;

    for (int i = 0; i < 817; i++)
            sum += n;

    printf("%.7g\n", sum);  
} 
  
int main(int argc, char **argv)
{ 
    float fa = 1.0f; 
    double da = 1.0; 
  
	printf("float\n");
    float_calc(fa); 
	
	printf("\ndouble\n");
    double_calc(da); 
    
    return 0; 
}   


/*
run:

float
8.336832

double
8.336735

*/

 



answered Dec 27, 2018 by avibootz

Related questions

1 answer 95 views
1 answer 163 views
1 answer 87 views
1 answer 108 views
1 answer 97 views
...