0

This is my first question, so I hope I explain this correctly ;-)

I need to create a method that takes in a double array, numbers, and returns a new array containing the square of each element in numbers. (Any help and guidance will be greatly appreciated. Thank you!)

The parameters I have been given are as follows:

<param name="numbers">Input array</param>
<returns>double array</returns>

        public double[] PowerArray(double[] numbers)

This is what I've written so far, but it returns double numbers. I need the return to contain decimal numbers.

        {
            double[] PowerArray = Array.ConvertAll(numbers, i => i * i);

            return PowerArray;
         }

My instructions are to return my answers following this example: double[] input = { -2.2, 0, 1.1, 3 } should return double[] [4.84, 0, 1.21. 9]. My return is giving me double[] [4.840000000000001, 0, 1.2100000000000002, 9].

Julie
  • 1
  • 3
  • 2
    I'm not sure what's wrong with your existing code. [It works just fine to me](https://dotnetfiddle.net/TASktN). What do you mean "return decimal"? Your method says it returns doubles. – gunr2171 Mar 18 '22 at 15:12
  • Thank you for your quick response!! Yes, it works; however, my instructions are to return my answers following this example: double[] input = { -2.2, 0, 1.1, 3 } should return double[] [4.84, 0, 1.21. 9]. My return is giving me double[] [4.840000000000001, 0, 1.2100000000000002, 9]. – Julie Mar 18 '22 at 15:17
  • 3
    [What Every Computer Scientist Should Know About Floating-Point Arithmetic](//docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – Pranav Hosangadi Mar 18 '22 at 15:24
  • 1
    [Is floating point math broken?](https://stackoverflow.com/questions/588004) This is how all floating point math works. If you want to avoid this floating point issue, don't use doubles, use decimals for everything. – gunr2171 Mar 18 '22 at 15:34
  • 1
    Since floating point numbers are inexact it is somewhat common to restrict the number of decimals when printing the numbers. So `(-2.2 * -2.2).ToString()` should give '4.84', while `(-2.2 * -2.2).ToString("R")` should give the full string, '4,8400000000000007'. The debugger may show the full number however. – JonasH Mar 18 '22 at 15:43

1 Answers1

0

You're hitting the fundamental nature of floating point math. Floating point numbers are approximations. You don't want an approximation, therefore it's inappropriate to use this data type.

Instead, use decimal to perform the math. You can then cast the result back to a double if you really need it to be.

using System.Linq;

// ...

public double[] PowerArray(double[] numbers)
{
    return numbers
        .Select(x => (decimal)x) // convert the element to a decimal
        .Select(x => x * x) // perform decimal-level multiplication
        .Select(x => (double)x) // convert the result back to a double
        .ToArray();
}
gunr2171
  • 16,104
  • 25
  • 61
  • 88
  • Thank you so much! That is working perfectly!! Your answer is much appreciated. I wish I could give it an upvote, but I am too new I guess. THANK YOU!!! :-) – Julie Mar 18 '22 at 22:20