locked
Converting a Delphi 6 Function to c# RRS feed

  • Question

  • User-1555856265 posted

    Hi guys. I have this simple function in Delphi

    function Round45 (Value : Real; RPower : Integer) : Real;
    begin
      Result := Trunc (Value * Power (10, RPower) + 0.5)/Power (10, RPower);
    end;

    It's purpose was to provide a result in the range from 0 to 100.

    I am attempting to convert that function to c#

    I have tried using Math.Round but that produces a double - I require an integer from 0 to 100.

    Any help would be appreciated.

    Wednesday, May 17, 2017 1:45 PM

Answers

  • User-1555856265 posted

    My previous works well for 2 and 20 questions, but fails with say 6 questions correct out of 13 questions.

    This new code corrects that error

    int result = Convert.ToInt32(Math.Round(((correctCount * 100f / questionCount) * Math.Pow(10, 10) + 0.5) / Math.Pow(10, 10)));
    

    Note the 100f which was all that was missing form my first attempt and removes the need for a double cast.

    Hope this info helps someone. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 18, 2017 9:38 AM
  • User-1555856265 posted

    Ahh, all it needed was the MidpointRounding parameter

    var val1 = Math.Round(correctCount * 100f / questionCount, MidpointRounding.AwayFromZero);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 25, 2017 11:21 AM

All replies

  • User-1555856265 posted

    Oh, it seems I can just use

    int result = Convert.ToInt32(correctAnswers * 100) / NoOfQuestions);

    So, for example, if I have 20 questions taken and the user obtains 9 correct answers, the code produces 45 which is just what I required.

    Wednesday, May 17, 2017 4:09 PM
  • User-1555856265 posted

    My previous works well for 2 and 20 questions, but fails with say 6 questions correct out of 13 questions.

    This new code corrects that error

    int result = Convert.ToInt32(Math.Round(((correctCount * 100f / questionCount) * Math.Pow(10, 10) + 0.5) / Math.Pow(10, 10)));
    

    Note the 100f which was all that was missing form my first attempt and removes the need for a double cast.

    Hope this info helps someone. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 18, 2017 9:38 AM
  • User303363814 posted

    Have you worked through, on paper, what your code is doing?

    If correctCount = 6 and questionCount = 13 then this is what happens (what is the type of these variables???)

    First multiply correctCount by 100 to get

    600

    Now divide by 13 to get, approximately

    46.15385

    Now you multiply by Mth.Pow(10,10) which is a huge number to get

    461,538,461,538.462

    Now you add 0.5 to get

    461,538,461,538.962

    now divide by the same huge number to get back to the number you started with (except for an amount less than the width of an electron)

    46.1538461538962

    and then you round it to get

    46

    and then you convert to Int32 to get

    46

    Wow!  That was a long journey.

    If the input variables are integers then

    int result = correctCount*100/questionCount

    will give the same result.

    If they are not integers then what are they?

    Monday, May 22, 2017 4:46 AM
  • User-1555856265 posted

    Hi Paul.

    The inputs are integers, so where as:

    using correctCount = 6 and questionCount = 13

    int result = (6 * 100) /13 gives a result of 46 which is correct when rounding 46.15

    but, if you use correctCount = 7 and questionCount = 13

    int rrsult = (7 * 100)/13 gives a result of 53 however rounding 53.84 shows the result should be 54.

    My code gives the correct answer of 54.

    So I don't think I can use simple integer maths.

    If you have a simple formula the gives 5/4 rounding of integers, please let me know.

    Monday, May 22, 2017 9:28 AM
  • User303363814 posted

    var result = Math.Round(correctCount*100f/questionCount);

    Wednesday, May 24, 2017 10:32 PM
  • User-1555856265 posted

    Thanks Paul. That seems to work. I shall have to fully test it.

    Thanks for the help and your time. 

    Thursday, May 25, 2017 6:39 AM
  • User-1555856265 posted

    Hi  Paul. I have tested your equation and mine in a double for loop going from a questionCount of 10 to 30 in the outer loop and a correctCount of 1 to the current questionCount

    in the inner loop.  This let me test all the permutations.

    Your code worked well in almost all loop iterations. the only discrepancy  was with the following values:

     1)  correctCount 2 and questionCount 16 gave a fraction 2/16 = 0.125 - my equation gave this as 13% whereas your equation gave it as 12%

     2) correctCount 10 and QuestionCount 16 gave a fraction 10/16 = 0.625 - my equation gave this  as 63% whereas your equation gave it 62%

     3) correctCount 3 and questionCount 24 gave a fraction 3/24 = 0.125 - my equation gave this as 13% whereas your equation gave it 12%

     4) correctCount 15 and questionCount 24 gave a fraction 15/24 = 0.625 - my equation gave this as 63% whereas your equation gave 62%

    You can see two of the problem values are 0.125 and 0.625.

    Anything that can be done to correct this rounding error? 

    Thursday, May 25, 2017 11:04 AM
  • User-1555856265 posted

    Ahh, all it needed was the MidpointRounding parameter

    var val1 = Math.Round(correctCount * 100f / questionCount, MidpointRounding.AwayFromZero);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 25, 2017 11:21 AM