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
#include <stdio.h>

// double has 2x the precision of float. 
// double has 15 decimal digits of precision, while float has 7.

void float_check() {
    float a = 0.01234f;
    float b = 0;
    
    for (int i = 0; i < 813; i++) {
        b += a;
    }
    
    printf("%.7g\n", b);
}

void double_check() {
    double a = 0.01234;
    double b = 0;
    
    for (int i = 0; i < 813; i++) {
        b += a;
    }
    
    printf("%.15g\n", b); 
}

int main() {
    float_check();
    double_check();
    
    return 0;
}

// calculator = 10.03242
 
/*
run:

10.03238
10.03242

*/
 

 



answered Nov 20, 2024 by avibootz

Related questions

1 answer 175 views
1 answer 162 views
1 answer 87 views
1 answer 108 views
1 answer 97 views
...