Notices
Results 1 to 10 of 10

Thread: In C# and C++, are conversion warnings important, and should attention be paid to them, or should they be ignored?

  1. #1 In C# and C++, are conversion warnings important, and should attention be paid to them, or should they be ignored? 
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    warning C4305: 'argument' : truncation from 'double' to 'float'

    Warnings like this

    and conversion from int to float, int to double, float to double, etc...

    Assuming im not aiming for:
    accuracy,
    optimizing performance,
    nor memory usage,
    are there any possible OTHER side effects from just ignoring these stuff?

    btw, i have got over 550 conversion warnings..!!


    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  2.  
     

  3. #2  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    I also have a question on how to truncate:
    the integer (rounded off a float).

    i've done this,
    Code:
    float myNum = 9.12345f;
    int theIntTruncation = myNum;
    float remainding = myNum - theIntTruncation;
    But there are the conversion warnings coming out (see OP)

    i've tried using cmath::truc(click), but then i think its not backward compatible with some versions..

    is there any way to solve this issue without warnings? or do i just leave my code as it is and ignore those annoying warnings?

    Thanks for reading through, please reply if you know of an answer. Thanks!!


    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  4. #3  
    WYSIWYG Moderator marnixR's Avatar
    Join Date
    Apr 2007
    Location
    Cardiff, Wales
    Posts
    5,760
    i'd do something like :

    Code:
    float myNum = 9.12345f;
    int theIntTruncation = (int)myNum;
    float remainding = myNum - theIntTruncation;
    that'll work in c#, don't know for sure about c++
    "Reality is that which, when you stop believing in it, doesn't go away." (Philip K. Dick)
    Reply With Quote  
     

  5. #4  
    ▼▼ dn ʎɐʍ sıɥʇ ▼▼ RedPanda's Avatar
    Join Date
    Aug 2012
    Location
    UK
    Posts
    2,737
    The warnings are a code smell.

    Code smell - Wikipedia, the free encyclopedia
    "In computer programming, code smell is any symptom in the source code of a program that possibly indicates a deeper problem. Code smells are usually not bugs—they are not technically incorrect and do not currently prevent the program from functioning. Instead, they indicate weaknesses in design that may be slowing down development or increasing the risk of bugs or failures in the future."


    Quote Originally Posted by marnixR View Post
    Code:
    float myNum = 9.12345f;
    int theIntTruncation = (int)myNum;
    float remainding = myNum - theIntTruncation;
    Although that code will compile without warnings/errors, the results will not always be correct.

    Try stepping through this code and you will see the problems:
    Code:
    float myNum = float.MaxValue;
    int theIntTruncation = (int)myNum;
    float remainding = myNum - theIntTruncation;
    int ranges from -2,147,483,648 to 2,147,483,647.
    float ranges from -3.4 1038 to +3.4 1038


    Once the float variable goes above 2,147,483,647 the code will start producing incorrect results.

    Quote Originally Posted by RamenNoodles View Post
    Assuming im not aiming for:
    accuracy,
    That would be your primary mistake.
    SayBigWords.com/say/3FC

    "And, behold, I come quickly;" Revelation 22:12

    "Religions are like sausages. When you know how they are made, you no longer want them."
    Reply With Quote  
     

  6. #5  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    Wow i didn't think until the scope.

    That's nice info to know. So is (int) type casting the proper way to truncate?
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  7. #6  
    ▼▼ dn ʎɐʍ sıɥʇ ▼▼ RedPanda's Avatar
    Join Date
    Aug 2012
    Location
    UK
    Posts
    2,737
    Quote Originally Posted by RamenNoodles View Post
    Wow i didn't think until the scope.

    That's nice info to know. So is (int) type casting the proper way to truncate?
    I would use something like
    Code:
    float myNum = 123.456F;
    float theIntTruncation = (float)Math.Floor(myNum);
    float remainding = myNum - theIntTruncation;
    but there are floating point errors expected with these kinds of operations.
    For example, the above code returns a value of 0.456001282.

    Even this code
    Code:
    float result = 123.456F - 123;
    returns a result of 0.456001282

    Some people take the route of converting the float to a string; extracting the part after the decimal separator and then prefixing it with a "0.".
    It works, even if it is a bit of a hack.

    If you fancy a detailed read about floating points: What Every Computer Scientist Should Know About Floating-Point Arithmetic

    But I guess I am drifting off-topic.
    Last edited by RedPanda; July 31st, 2014 at 09:35 AM.
    SayBigWords.com/say/3FC

    "And, behold, I come quickly;" Revelation 22:12

    "Religions are like sausages. When you know how they are made, you no longer want them."
    Reply With Quote  
     

  8. #7  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    In modern computers, it's a pretty good idea to always use doubles unless you really need the extra memory. As far as I know, there's no significant speed difference between the two anymore. Floating point errors are proportional to the low bits of the mantissa, which is much smaller with doubles.

    But yeah, any time you're working with floating point numbers (floats or doubles) you just have to expect some floating point errors to creep in. The best you can do is pay attention to things like warnings and be careful not to do things that would magnify those errors. (One reason you shouldn't compare two floats with ==, which I don't think will even give you a warning though it probably should.)
    Reply With Quote  
     

  9. #8  
    WYSIWYG Moderator marnixR's Avatar
    Join Date
    Apr 2007
    Location
    Cardiff, Wales
    Posts
    5,760
    Quote Originally Posted by RedPanda View Post
    Although that code will compile without warnings/errors, the results will not always be correct.
    it all depends whether the rounding off error is something you can live with for the purpose you have in mind
    for many house-and-garden web page applications it's usually not an issue
    if you need engineering/scientific accuracy to multiple decimal places or work with really large numbers then you have to take more care
    "Reality is that which, when you stop believing in it, doesn't go away." (Philip K. Dick)
    Reply With Quote  
     

  10. #9  
    ▼▼ dn ʎɐʍ sıɥʇ ▼▼ RedPanda's Avatar
    Join Date
    Aug 2012
    Location
    UK
    Posts
    2,737
    Quote Originally Posted by marnixR View Post
    Quote Originally Posted by RedPanda View Post
    Although that code will compile without warnings/errors, the results will not always be correct.
    it all depends whether the rounding off error is something you can live with for the purpose you have in mind
    for many house-and-garden web page applications it's usually not an issue
    Yes, which is why it is a Code Smell rather than a bug.

    Quote Originally Posted by marnixR View Post
    if you need engineering/scientific accuracy to multiple decimal places or work with really large numbers then you have to take more care
    But it can be avoided completely by simply not casting it to an Int.
    And avoiding Code Smells is a good habit to learn.

    I understand the need for the occasional hack when time is short - but this is not one of those times.
    It takes as long to do it correctly as it does to do it incorrectly.
    SayBigWords.com/say/3FC

    "And, behold, I come quickly;" Revelation 22:12

    "Religions are like sausages. When you know how they are made, you no longer want them."
    Reply With Quote  
     

  11. #10  
    Forum Professor river_rat's Avatar
    Join Date
    Jun 2006
    Location
    South Africa
    Posts
    1,497
    In a enterprise type environment we treat all warnings as errors in our build (c++ compiler option /WX) and while you are at it think about maintaining ANSI c/c++ compliance (c++ compiler option /Za) which increases your chances of getting a gcc build working in the future without causing grievous harm to your own psyche.
    As is often the case with technical subjects we are presented with an unfortunate choice: an explanation that is accurate but incomprehensible, or comprehensible but wrong.
    Reply With Quote  
     

Similar Threads

  1. Paid to Google
    By Theoryofrelativity in forum General Discussion
    Replies: 3
    Last Post: July 17th, 2008, 11:58 AM
  2. What is the most highly paid job?
    By =CtrlAltDelete= in forum Business & Economics
    Replies: 13
    Last Post: May 31st, 2008, 09:32 PM
  3. What can a mathematician get paid to do?
    By Demen Tolden in forum Mathematics
    Replies: 4
    Last Post: January 24th, 2008, 10:07 AM
Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •