2

I'm learning C programming, so during writing my code, one question emerged in my head. I'm writing program which works with numbers featuring floating point, but it does not require so big mantissa as double variable provides. As far as I know, due to latest standards, C compilers automatically convert float variables to double. So I was wondering if C syntax that prohibits converting my variables defined as float, to double exist. Is it compiler defined functionality or C syntax features such conversion definition? I'm using GCC by the way, if it matters.

dragonhaze
  • 189
  • 1
  • 8
  • Why are you using float, not double? You give a reason why you may be able to get away with using float, but no positive reason for preferring it. – Patricia Shanahan Nov 29 '19 at 03:18
  • 1
    `As far as I know, due to latest standards, C compilers automatically convert float variables to double` this is **wrong**. See [How is float variable auto-promoted to double type?](https://stackoverflow.com/q/12118738/995714). Compilers are **allowed** to do math in higher precision if it's more efficient though, depending on the value of [FLT_EVAL_METHOD](https://en.cppreference.com/w/c/types/limits/FLT_EVAL_METHOD) – phuclv Nov 29 '19 at 03:26
  • the answer is to set FLT_EVAL_METHOD to 0. There are a lot of duplicates: [What precision are floating-point arithmetic operations done in?](https://stackoverflow.com/q/25302480/995714), [C99 floating point intermediate results](https://stackoverflow.com/q/50356941/995714), [May C evaluate with multiple floating-point formats?](https://stackoverflow.com/q/49246014/995714) – phuclv Nov 29 '19 at 03:37
  • Does this answer your question? [What precision are floating-point arithmetic operations done in?](https://stackoverflow.com/questions/25302480/what-precision-are-floating-point-arithmetic-operations-done-in) – phuclv Nov 29 '19 at 03:37
  • 2
    Perhaps the most surprising (among many) pitfalls for beginners is a `float` type, say: `x`, and an expression of the form: `x + 1.0`, which is promoted to `double`. Use: `x + 1.0f` for the desired result. – Brett Hale Nov 29 '19 at 06:40
  • Looking at [CERT C](https://wiki.sei.cmu.edu/confluence/display/c/SEI+CERT+C+Coding+Standard) standards might help you identify most common mistakes and how to mitigate them. [Rules](https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=87152181), [Recommendations](https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=87151969) – Brett Hale Nov 29 '19 at 06:44

0 Answers0