The shortcomings of C: (iv) floats and ints

What usefulness is the float in C? To help conserve storage? In many cases, floating-point arithmetic defaults to double, so why even include it if the results it provides are sub-standard? It is almost misleading to have a type which exists, but whose operations are performed in terms of second type. The sqrt() function in the math.h library in fact defaults to double, but provides sqrtf() and sqrtl() for floats and long doubles. This of course leads to a discourse on the choice of names for these non-integer types.

float 
double
long double

They derive their behaviour from C’s predecessor, and someone thought it was a good idea. A float does make sense or even the term real. Beyond that why not use something that is easier to determine. The term double means double the precision of a float, which is meaningless if double is the standard floating-point number. The term long double is confusing because long is also used with int‘s.

Fortran makes more sense, where you can specify the number of significant digits of precision, and the exponent range. For example:

integer, parameter :: r8=selected_real_kind(15,307)
real(kind=r8) :: r

Here the variable r is 15 significant digits, and an exponent range of 307. is Even in the context of integers, one can specify the number of bytes (which let’s face it, is pretty standard now. Here’s a code snippet declaring a 2-byte, and 8-byte integer in Fortran:

! 2-byte integer
integer(kind=2) :: shortval
! 8-byte integer
integer(kind=8) :: longval

At least with other languages the terminology is kept consistent.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.