This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Inacurate precision truncating.

Hi folk,
I have

u16 wTemp = 0;
fsingle fTemp = 17.5123;

wTemp = (u16)fTemp;

Somehow, the wTemp is 17. Technically, the wTemp should be 18, do anybody know what do I have to do for the compiler in order to generate more precise data conversion???

Thanks!

Parents Reply Children
  • Good to see people who aren't afraid of the Ternary Operator!

  • No, precedence is your friend; explicitness is my friend.

    :-)

    Over the years, and especially working with clients having pre-existing code, I've found (for me at least) that it's best not to pick nits over stylistic differences. To each his (or her) own. So, while I generally appreciate your informed posts, I won't engage you in discussing our sylistic differences.

    Keep up the good work. We all appreciate your help around here.

    --Dan Henry

  • I was definitely just ribbing you. I agree, to each his own.

    - Mark

  • Indeed, I always tell people who say it obfuscates code that var++; is very odd at first glance as is var += value; ?: is very, very clear once the idiom is learned just as are var++ and var += value.

    Long live all parts of the the C language.

    Example use #234 of ?:...

    int cash = 1;
    printf("You have %d dollar%s\n", cash, 1 == cash ? "" : "s");
    - Mark

  • Of course this would have worked just as well I suppose:

    int cash = 1;
    char dollarStr[][sizeof "dollars"] = { "dollars", "dollar" };
    
    printf("You have %d %s\n", cash, dollarStr[cash == 1]);
    - Mark

  • True that folks, there is no need to talk about programming styles.
    In general, our team follow some sort of programming style pattern. This is due to programming skill differences among team members. So in general, making the program source code as explicitly as possible so that there is no misunderstanding and in addition, the C compiler will be smart enought to optimize "all" redundency.

  • "the C compiler will be smart enought to optimize "all" redundency."

    You'd hope so, wouldn't you?

    I did once come across a compiler (not 8051; not Keil; someone beginning with 'I') which did produce different results if you used "unnecessary" parentheses!

    But then their assembler produced the same opcode for both the "Jump If Equal" and "Jump If Not Equal" mnemonics!!