1

I was commenting on Why should I always enable compiler warnings? and pointed out

In the embedded world, the warnings that worry me most are "possible loss of precision" and "comparison between signed and unsigned" warnings.

I find it difficult to grasp how many "programmers" ignore these (in fact, I am not really sure why they are not errors)

Can anyone explain why trying to put a possible quart into a certified pint pot is not treated as an error? Surely it's just a disaster waiting to happen?

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • 6
    Facetiously, because is only *possible*. C and C++ assume you know what you're doing. Sometimes mixing types is necessary, e.g. working with histograms when you convert a floating point to an array index. – Bathsheba Sep 10 '19 at 07:36
  • 1
    Even if I have a loss of precision, it is not a failure. If I need 10digits and reduce from 14 to 12, why that should be an error? – Klaus Sep 10 '19 at 07:47
  • @klaus: the usual conversions are from double to float or to int, or from int to short. That can be a problem or not. –  Sep 10 '19 at 07:50
  • In addition, floating-point calculations are not so accurate. therefore, there always be a lack of precision. – SHR Sep 10 '19 at 07:50
  • 1
    Because the pint pot is so large that if it ever fills up it means something else is almost certainly already broken – Tharwen Sep 10 '19 at 08:49
  • A good point, and one for which I always add code checks. – Mawg says reinstate Monica Sep 10 '19 at 09:31
  • @Lundin at least they already recognized the initial "everything as auto" promotion was not that great idea... :) – Ped7g Sep 10 '19 at 12:46

1 Answers1

4

The message says "possible". Only the programmer can know if this is a true issue or not; the compiler cannot guess.

"Surely it's just a disaster waiting to happen?" is an overstatement. Some loss of precision can just be harmless. I would rather fear over/underflow. Usually you know what you are doing with your data types, the compiler warning rather sounds as a kind reminder.

  • When I know that it is not an error, I generally code a function to preform the conversion, in such a way that it generates no compiler warnings ***and*** checks for errors, mjust in case – Mawg says reinstate Monica Sep 10 '19 at 07:44
  • @Mawg: casts do remove the warning, as should. Systematically checking for loss of accuracy can add intolerable overhead. –  Sep 10 '19 at 07:45
  • 1
    Oops: back in my box. – Bathsheba Sep 10 '19 at 08:04
  • @Mawg yes, in ideal world one can add cast or extra `&` to make it clear to compiler that some loss of precision is expected and ok. In ideal world also people don't produce memory leaks, don't access invalid pointers, and don't need programming languages infected by Garbage Collector to get away with code which doesn't truly bother about memory usage and everyone would be C++ guru... But here we are, on planet Earth, where none of that is true. You are still free to rewrite all the SW in the world in the clean modern C++, without warnings. I will be personally very thankful if you do that. – Ped7g Sep 10 '19 at 08:28