Test float/double Type in Dev-C ++

Source: Internet
Author: User

Kingwei 2005.3.11

Tutorial environment: Dev-C ++ 4.9.6.0 (gcc/mingw32), use the-Wall compilation Option

# Include <stdio. h>

Int main ()
{
Float v_float;
Double v_double;
Long double v_long_double;

Printf ("sizeof (float) = % u/n", sizeof (float ));
Printf ("sizeof (double) = % u/n", sizeof (double ));
Printf ("sizeof (long double) = % u/n", sizeof (long double ));
 
/*-3.40282e + 038 ~ + 3.40282e + 038 */
Scanf ("% f", & v_float );
Printf ("% f/n", v_float );
Printf ("% e/n", v_float );

/*-1.79769e + 308 ~ + 1.79769e + 308 */
Scanf ("% lf", & v_double );
Printf ("% f/n", v_double );
Printf ("% e/n", v_double );
 
/*-1.79769e + 308 ~ + 1.79769e + 308 */
Scanf ("% Lf", & v_long_double );
Printf ("% Lf/n", v_long_double );
Printf ("% Le/n", v_long_double );

Return 0;
}

1. float, double, and long double are the following lengths:

Sizeof (float) = 4
Sizeof (double) = 8
Sizeof (long double) = 12

2. Numerical range test

Float +/-3.40282e + 038
Double +/-1.79769e + 308
Long double ++/-1.79769e + 308

----- Test case #1 positive Extreme Value -----

3.40282e + 038
1.79769e + 308
1.79769e + 308

Output:

340282001837565600000000000000000000000.000000
3.402820e + 038
179769000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
1.797690e + 308
179769000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
1.797690e + 308

----- Test case #2 negative Extreme Value -----

-3.40282e + 038
-1. 79769e + 308
-1. 79769e + 308

Output:

-340282001837565600000000000000000000000.000000
-3.402820e + 038
-179769000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
-1. 797690e + 308
-179769000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
-1. 797690e + 308

----- Test case #3 forward overflow -----

3.40283e + 038
1.79770e + 308
1.79770e + 308

Output:

1. # INF00
1. # INF00e + 000
1. # INF00
1. # INF00e + 000
1. # inf00
1. # inf00e + 000

----- Test case #4 negative overflow -----

-3.40283e + 038
-1. 79770e + 308
-1. 79770e + 308

Output:

-1. # inf00
-1. # inf00e + 000
-1. # inf00
-1. # inf00e + 000
-1. # inf00
-1. # inf00e + 000

It can be seen that although long double is 4 bytes longer than double, the value range is the same.
The length, precision, and range of the Long double type are related to the compiler and operating system used.

In VC ++ 6.0, the IEEE standard floating point number is used. Long double is 80-bit in length and the value range is about +/-1.2e + 4932,
Printf format: % lf, % Le, % LG. (not verified)

3. Floating Point parameter pressure stack rules: Float (4 bytes) type extended to double (8 bytes) into the stack.

Therefore, in the input, float (% F) and double (% lf) must be distinguished. In the output, use % F,
The printf function outputs the float (extended to double) and double data pushed into the stack according to the double-type rules.
If the % lf format character is specified during output, the GCC compiler will give a warning.

4. The GCC compiler can select the float length, whether it is consistent with double.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.