CS156: Intro to C, Part I

Spring 2018

Float Vs Double

See this page as a slide show

CS156 Float Vs Double

Overview

float f = 1.0e9;
double d = 1.0e9;
f += 123.456789;
d += 123.456789;
printf("%f\n", f);
printf("%f\n", d);
1000000128.000000
1000000123.456789

Calculations

Calculations are generally carried out with double precision.

float f = 1.0e9;
double d = f + 123.456789;
printf("%f\n", d);
1000000123.456789

scanf

scanf needs to know the difference between float and double:

float f;
double d;
scanf("%f %lf", &f, &d);

printf

float f = 1.2;
double d = 3.4;
printf("%f\n", f);
printf("%f\n", d);
printf("%lf\n", f);
printf("%lf\n", d);
1.200000
3.400000
1.200000
3.400000

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-02-26T12:14

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building