MersenneTwister-Lab / TinyMT

Tiny Mersenne Twister
Other
210 stars 37 forks source link

Warning: Implicit Cast uint->float #6

Closed ax3l closed 4 years ago

ax3l commented 4 years ago

Found in https://github.com/alpaka-group/alpaka/issues/994 by @krzikalla

These lines https://github.com/MersenneTwister-Lab/TinyMT/blob/9d7ca3c16100d51b5f27afd26825bc806b9189f6/tinymt/tinymt32.h#L181-L184

throw a -Wconversion warning:

tinymt32.h: warning: conversion to ‘float’ from ‘uint32_t {aka unsigned int}’ may alter its value [-Wconversion]
     return (tinymt32_temper(random) >> 8) * TINYMT32_MUL;
        ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~

Possible fix via explicit cast:

 return float(tinymt32_temper(random) >> 8) * TINYMT32_MUL;
majiang commented 4 years ago

possible fix LGTM

ax3l commented 4 years ago

@krzikalla do you want to open a PR to this repo?

MSaito commented 4 years ago

In my environment, clang 11.03, -Wconversion option makes many warnings. I think no warnings with -Wall is enough.

ax3l commented 4 years ago

I think I fixed all other conversion warnings in https://github.com/alpaka-group/alpaka/pull/579 If you like I can provide a PR for those as well, many downstream codes compile with -Wall -Wextra these days to catch conversion issues in their own code (e.g. catching float/int precision bugs from accidental casts).

It's not too verbose to add those and makes the intention to cast to readers a bit more clear.

MSaito commented 4 years ago

Thank you, Axel. I will welcome your PR.

ax3l commented 4 years ago

Thank you for merging all PRs, the CI and the leftover c6318794da0d0b89546e0ca26b1fff0e9a59f8df :tada: