CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2016
    Posts
    2

    reason for a code written in C and matlab giving different output

    The codes (C and matlab) are exactly same (operations wise) and same value of epsilon is used in both the codes. Still they give different output, what could be the reason for it.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: reason for a code written in C and matlab giving different output

    1. What is "epsilon"?
    2. What is the type of the "output"?
    3. What was the "difference" between the "outputs"?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2016
    Posts
    2

    Re: reason for a code written in C and matlab giving different output

    1.This epsilon is used in if statements for making comparisons. epsilon in matlab is 2.2204e-16 .

    I use " #define eps 2.2204e-16 " in C

    2. The output is obtained by using arithmetic operations and comparisons on the initial data (which is taken to be exactly same for both C and Matlab code)

    3. The contour plots of C and Matlab outputs differ slightly

  4. #4
    Join Date
    Aug 2006
    Posts
    231

    Re: reason for a code written in C and matlab giving different output

    Quote Originally Posted by tangled09 View Post
    3. The contour plots of C and Matlab outputs differ slightly
    How slightly? Some numbers can't be represented exactly, so they will be rounded off. The deviation can then increase for every arithmetic operation.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: reason for a code written in C and matlab giving different output

    What precision are you using for real numbers in c and in Matlab? In c/c++ there are several different precision types that can be used - eg float, double, long double.

    For MS VS, see
    https://msdn.microsoft.com/en-us/library/cc953fe1.aspx
    https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx

    Also note that in c the ordering of expression evaluation can have an effect on the accuracy of the result.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured