Thread: C standards question

  1. #1
    .
    Join Date
    Nov 2003
    Posts
    307

    C standards question

    I have a copy of the ISO C99 standards.

    On another forum I found -
    <snip>
    In fact for languages where the standard specifies that all arithemetic is done internally in double precision (e.g. ANSI C), single precision can run slower than double because of the extra conversions ....<snip>
    The thread is discussing preformance differences for single vs double precision.

    Anyway I cannot manage to find something in the standard to support this.
    I'm guessing it's me.

    Are all floats promoted to double by standard? Help.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    A decimal constant such as 1.0 is double by default, but this can be overriden by suffixing with f or F for float, or l or L for long double.

    Edit: Also, in C the functions in <math.h> have double arguments and values, so it's fairly difficult to do anything in any other precision, but I don't think the standard itself mandates what you read.

    Edit: I just read that in C99 float and long double versions were added to the functions in <math.h>. What I wrote holds for C90 only.

    http://en.wikibooks.org/wiki/C_Programming/Further_math
    Last edited by robatino; 09-14-2007 at 08:23 AM.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    From "The C Programming Language" 2nd Edition - Appendix A6.5
    "[...] arithmetic on float operands may be done in single precision, rather than double; the first edition specified that all floating arightmetic was double precision."
    So, yes, that was true in the first edition, but no longer. Ansi C performs arithmetic on floats in single precision, hence no performance difference.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I doubt that the standard would require all arithemetic to be done in double precision, because the standard tries to be as multi-platform as possible, and such a requirement would reduce the portability. However, it is certainly allowed, and I would not be surprise that most modern compilers followed this convention.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by King Mir View Post
    I doubt that the standard would require all arithemetic to be done in double precision, because the standard tries to be as multi-platform as possible, and such a requirement would reduce the portability. However, it is certainly allowed, and I would not be surprise that most modern compilers followed this convention.
    Some compilers may. I know for a fact that gcc and microsoft VC++ for x86/x64_DO_ make a difference between float and double, and calculations are performed at the relevant precision, float being slightly faster than double in many cases, and never slower. There isn't much difference unless you also manage to get the compiler to generate SSE SIMD instructions, where there is a theoretical improvement of 2 vs 4x for double vs. float [neither of these compilers do SSE very well, so the performance benefit is sometimes small and sometimes worse than the original code].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    .
    Join Date
    Nov 2003
    Posts
    307
    I don't see anything in the standard to support all fp operations being double precision.
    It did not sound at all right to start with, but I've gotten rather old, and so wanted to be sure.

    Thanks.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The comment you ran across may have been in regard to C90 library functions from <math.h>.
    The header <math.h> declares several mathematical functions and defines one macro. The functions take double-precision arguments and return double-precision values.
    Section 7.12 in C99 now reads:
    The header <math.h> declares two types and many mathematical functions and defines several macros. Most synopses specify a family of functions consisting of a principal function with one or more double parameters, a double return value, or both; and other functions with the same name but with f and l suffixes, which are corresponding functions with float and long double parameters, return values, or both.
    C99 added IEC 60559 arithmetic support in the normative Annex F, as mentioned in the C99 Foreward.
    C99 allows an implementation to pre-define the __STD_IEC_559 preprocessor macro, indicating that it conforms to certain required behavior of the IEC 60559 (a.k.a. IEEE 599) specification regarding floating-point arithmetic and library functions. Implementations that do not pre-define this macro are not require to provide conforming floating-point behavior.
    FWIW
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    .
    Join Date
    Nov 2003
    Posts
    307
    Actually the comment flatly stated that C standards dictate that fp operations must all be double precision. I could no imagine how that would EVER get through the standards committee.

    But you're right it turns out he got the idea from Pflauger's book on the standard library.
    Not the standards at all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM