On 17.08.2011 14:30, MacArthur, Ian (SELEX GALILEO, UK) wrote:

>>> That's quite interesting though - it is spotting that 0.1 can't be
>>> represented in a float and complaining, but it is OK with the other
>>> values that can be represented.
>>> That said, I don't think you can represent 0.1 as a double either...
>>> Maybe there's a tolerance margin...
>>>
>>> Still, seems odd. Maybe that's too high a warning level? I
>> don't use the
>>> VC tools so have no real opinion (or knowledge) about them.
>>
>> No miracles. You can't represent 0.1 with zero error in any binary
>> float format. Always exists small difference from the exact value.
>> (There is no problem for 1, 0.5, 0.25 and others).
>> Thus 0.1F != 0.1LF and the compiler reports about wrong cast of type.
>
> Yup - I know that, and I guess Albrecht does too;

sure

> my point was rather
> that I found it curious that it only warns on the inexact conversion,
> but not on the exact conversions.

Exactly my point, too.

> I'd have thought the "easy" case for the compiler is just to treat all
> these implicit double-to-float conversions as worthy of comment, but
> here it is clearly only warning about the case where there will be some
> (additional) loss of precision.

Of course, MS never use the "easy" way... ;-)

Well, kidding aside: of course users *would* complain if there were
warnings about "possible precision loss" for constants like 0.5, so
what should they do? In one case there was (shown as a diff of my
"fix"):

-  {0.8660254, 0.5, 1, 0};
+  {0.8660254f, 0.5f, 1.0f, 0.0f};

There was only one warning concerning this line, but I decided to add
the suffix 'f' to all constants. Makes the code much less readable IMO.

Another question: Is 0.8660254 representable in single precision at
all? AFAICT there are *about* 7 decimal digits representable in a float,
depending on the float's implementation (IEEE in most cases, today),
so this is a border line case anyway. Simple test:

$ cat x.c; gcc -mconsole -o x x.c && ./x
#include <stdio.h>
static float a = 0.8660254f;
static double d = 0.8660254d;
int main() {
   printf ("\nResult: a = %-14.12f, d = %-14.12g\n",a,d);
}

Result: a = 0.866025388241, d = 0.8660254

> In any case, it seems to me to be slightly spurious, as it is clear (to
> us humanoids) from the context that float is intended - though the
> compiler can't assume that of course.
> Maybe we should just add the trailing "f" qualifiers and make the
> problem go away once and for all...

Maybe, but constants aren't the only cases...

FWIW, I'll append the glpuzzle diffs here. And that doesn't fix
/all/ warnings. As usual with Windows software there are other
warnings about "Argument" conversions, but they don't tell us
which one of many arguments would need a cast. I love that. ;-)

Albrecht

$ svn diff test/glpuzzle.cxx
Index: test/glpuzzle.cxx
===================================================================
--- test/glpuzzle.cxx   (revision 8976)
+++ test/glpuzzle.cxx   (working copy)
@@ -104,7 +104,7 @@
  static char ysize[PIECES + 1] =
  {0, 1, 1, 1, 1, 2, 2, 2, 2, 1, 2};
  static float zsize[PIECES + 1] =
-{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.6};
+{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.6f};

  static Config startConfig =
  {
@@ -160,30 +160,30 @@

  float boxcoords[][3] =
  {
-  {0.2, 0.2, 0.9},
-  {0.8, 0.2, 0.9},
-  {0.8, 0.8, 0.9},
-  {0.2, 0.8, 0.9},
-  {0.2, 0.1, 0.8},
-  {0.8, 0.1, 0.8},
-  {0.9, 0.2, 0.8},
-  {0.9, 0.8, 0.8},
-  {0.8, 0.9, 0.8},
-  {0.2, 0.9, 0.8},
-  {0.1, 0.8, 0.8},
-  {0.1, 0.2, 0.8},
-  {0.2, 0.1, 0.2},
-  {0.8, 0.1, 0.2},
-  {0.9, 0.2, 0.2},
-  {0.9, 0.8, 0.2},
-  {0.8, 0.9, 0.2},
-  {0.2, 0.9, 0.2},
-  {0.1, 0.8, 0.2},
-  {0.1, 0.2, 0.2},
-  {0.2, 0.2, 0.1},
-  {0.8, 0.2, 0.1},
-  {0.8, 0.8, 0.1},
-  {0.2, 0.8, 0.1},
+  {0.2f, 0.2f, 0.9f},
+  {0.8f, 0.2f, 0.9f},
+  {0.8f, 0.8f, 0.9f},
+  {0.2f, 0.8f, 0.9f},
+  {0.2f, 0.1f, 0.8f},
+  {0.8f, 0.1f, 0.8f},
+  {0.9f, 0.2f, 0.8f},
+  {0.9f, 0.8f, 0.8f},
+  {0.8f, 0.9f, 0.8f},
+  {0.2f, 0.9f, 0.8f},
+  {0.1f, 0.8f, 0.8f},
+  {0.1f, 0.2f, 0.8f},
+  {0.2f, 0.1f, 0.2f},
+  {0.8f, 0.1f, 0.2f},
+  {0.9f, 0.2f, 0.2f},
+  {0.9f, 0.8f, 0.2f},
+  {0.8f, 0.9f, 0.2f},
+  {0.2f, 0.9f, 0.2f},
+  {0.1f, 0.8f, 0.2f},
+  {0.1f, 0.2f, 0.2f},
+  {0.2f, 0.2f, 0.1f},
+  {0.8f, 0.2f, 0.1f},
+  {0.8f, 0.8f, 0.1f},
+  {0.2f, 0.8f, 0.1f},
  };

  float boxnormals[][3] =
@@ -194,26 +194,26 @@
    {0, 0, -1},
    {0, -1, 0},
    {-1, 0, 0},
-  {0.7071, 0.7071, 0.0000},  /* 6 */
-  {0.7071, -0.7071, 0.0000},
-  {-0.7071, 0.7071, 0.0000},
-  {-0.7071, -0.7071, 0.0000},
-  {0.7071, 0.0000, 0.7071},  /* 10 */
-  {0.7071, 0.0000, -0.7071},
-  {-0.7071, 0.0000, 0.7071},
-  {-0.7071, 0.0000, -0.7071},
-  {0.0000, 0.7071, 0.7071},  /* 14 */
-  {0.0000, 0.7071, -0.7071},
-  {0.0000, -0.7071, 0.7071},
-  {0.0000, -0.7071, -0.7071},
-  {0.5774, 0.5774, 0.5774},  /* 18 */
-  {0.5774, 0.5774, -0.5774},
-  {0.5774, -0.5774, 0.5774},
-  {0.5774, -0.5774, -0.5774},
-  {-0.5774, 0.5774, 0.5774},
-  {-0.5774, 0.5774, -0.5774},
-  {-0.5774, -0.5774, 0.5774},
-  {-0.5774, -0.5774, -0.5774},
+  {0.7071f, 0.7071f, 0.0000f},  /* 6 */
+  {0.7071f, -0.7071f, 0.0000f},
+  {-0.7071f, 0.7071f, 0.0000f},
+  {-0.7071f, -0.7071f, 0.0000f},
+  {0.7071f, 0.0000f, 0.7071f},  /* 10 */
+  {0.7071f, 0.0000f, -0.7071f},
+  {-0.7071f, 0.0000f, 0.7071f},
+  {-0.7071f, 0.0000f, -0.7071f},
+  {0.0000f, 0.7071f, 0.7071f},  /* 14 */
+  {0.0000f, 0.7071f, -0.7071f},
+  {0.0000f, -0.7071f, 0.7071f},
+  {0.0000f, -0.7071f, -0.7071f},
+  {0.5774f, 0.5774f, 0.5774f},  /* 18 */
+  {0.5774f, 0.5774f, -0.5774f},
+  {0.5774f, -0.5774f, 0.5774f},
+  {0.5774f, -0.5774f, -0.5774f},
+  {-0.5774f, 0.5774f, 0.5774f},
+  {-0.5774f, 0.5774f, -0.5774f},
+  {-0.5774f, -0.5774f, 0.5774f},
+  {-0.5774f, -0.5774f, -0.5774f},
  };

  int boxfaces[][4] =
@@ -307,30 +307,30 @@

  float containercoords[][3] =
  {
-  {-0.1, -0.1, 1.0},
-  {-0.1, -0.1, -0.1},
-  {4.1, -0.1, -0.1},
-  {4.1, -0.1, 1.0},
-  {1.0, -0.1, 0.6},     /* 4 */
-  {3.0, -0.1, 0.6},
-  {1.0, -0.1, 0.0},
-  {3.0, -0.1, 0.0},
-  {1.0, 0.0, 0.0},      /* 8 */
-  {3.0, 0.0, 0.0},
-  {3.0, 0.0, 0.6},
-  {1.0, 0.0, 0.6},
-  {0.0, 0.0, 1.0},      /* 12 */
-  {4.0, 0.0, 1.0},
-  {4.0, 0.0, 0.0},
-  {0.0, 0.0, 0.0},
-  {0.0, 5.0, 0.0},      /* 16 */
-  {0.0, 5.0, 1.0},
-  {4.0, 5.0, 1.0},
-  {4.0, 5.0, 0.0},
-  {-0.1, 5.1, -0.1},    /* 20 */
-  {4.1, 5.1, -0.1},
-  {4.1, 5.1, 1.0},
-  {-0.1, 5.1, 1.0},
+  {-0.1f, -0.1f, 1.0f},
+  {-0.1f, -0.1f, -0.1f},
+  {4.1f, -0.1f, -0.1f},
+  {4.1f, -0.1f, 1.0f},
+  {1.0f, -0.1f, 0.6f},     /* 4 */
+  {3.0f, -0.1f, 0.6f},
+  {1.0f, -0.1f, 0.0f},
+  {3.0f, -0.1f, 0.0f},
+  {1.0f, 0.0f, 0.0f},      /* 8 */
+  {3.0f, 0.0f, 0.0f},
+  {3.0f, 0.0f, 0.6f},
+  {1.0f, 0.0f, 0.6f},
+  {0.0f, 0.0f, 1.0f},      /* 12 */
+  {4.0f, 0.0f, 1.0f},
+  {4.0f, 0.0f, 0.0f},
+  {0.0f, 0.0f, 0.0f},
+  {0.0f, 5.0f, 0.0f},      /* 16 */
+  {0.0f, 5.0f, 1.0f},
+  {4.0f, 5.0f, 1.0f},
+  {4.0f, 5.0f, 0.0f},
+  {-0.1f, 5.1f, -0.1f},    /* 20 */
+  {4.1f, 5.1f, -0.1f},
+  {4.1f, 5.1f, 1.0f},
+  {-0.1f, 5.1f, 1.0f},
  };

  float containernormals[][3] =
@@ -719,8 +719,8 @@
  found_piece:
    if (!movingPiece) {
      movingPiece = movedPiece;
-    move_x = fromx;
-    move_y = fromy;
+    move_x = float(fromx);
+    move_y = float(fromy);
    }
    move_x += xadds[movedir] * MOVE_SPEED;
    move_y += yadds[movedir] * MOVE_SPEED;
@@ -1024,8 +1024,8 @@
      while (selecty > 0 && thePuzzle[selecty - 1][selectx] == 
movingPiece) {
        selecty--;
      }
-    move_x = selectx;
-    move_y = selecty;
+    move_x = float(selectx);
+    move_y = float(selecty);
      selected = 1;
      selstartx = selx;
      selstarty = sely;
@@ -1096,11 +1096,11 @@
        /* Allow visual movement of solution piece outside of the

           box */
-      move_x = selectx;
-      move_y = sely - selstarty + selecty;
+      move_x = float(selectx);
+      move_y = float(sely - selstarty + selecty);
      } else {
-      move_x = selectx;
-      move_y = selecty;
+      move_x = float(selectx);
+      move_y = float(selecty);
      }
    }
  }
@@ -1330,11 +1330,11 @@
    static float lmodel_local[] =
    {GL_FALSE};
    static float light0_ambient[] =
-  {0.1, 0.1, 0.1, 1.0};
+  {0.1f, 0.1f, 0.1f, 1.0f};
    static float light0_diffuse[] =
-  {1.0, 1.0, 1.0, 0.0};
+  {1.0f, 1.0f, 1.0f, 0.0f};
    static float light0_position[] =
-  {0.8660254, 0.5, 1, 0};
+  {0.8660254f, 0.5f, 1.0f, 0.0f};
    static float light0_specular[] =
    {0.0, 0.0, 0.0, 0.0};
    static float bevel_mat_ambient[] =
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to