csharp进阶练习题:计算复杂的对数函数【难度:2级】--景越C#经典编程题库,不同难度C#练习题,适合自学C#的新手进阶训练

csharp进阶练习题:计算复杂的对数函数【难度:2级】:

在任何给定** 复数计算复对数**,精确到10 ^ -12至少1.虚部应是间隔(-π,π](即如果虚部是完全π,保持它作为是)的内部.

注意:你不应该试图计算在两极这个函数的值.请返回null /NULL /nil /None(C#:抛出一个ArgumentException,爪哇:抛出ArithmeticException)如果发生这种情况.

`System.Numerics`在此习题不允许 - 揣摩公式/算法自己;)提示:通常的数学身份持有如此复杂的数字,以及实数(当然正整数)

编程目标:

using System;
public class Complex
{
  public static double[] Log(double[] z)
  {
    double x = z[0]; // Real part of z = x + iy
    double y = z[1]; // Imaginary part of z = x + iy
    // Return your computed result in the form new double[] {x, y}
  }
}


测试样例:

namespace Solution {
  using NUnit.Framework;
  using System;
  [TestFixture]
  public class LogTest
  {
    protected static void AssertComplexEquals(double[] expected, [] actual)
    {
       (.([])  ) .([], .(expected[0]).Within(1e-12), "The real part of your returned complex number is not sufficiently close to the expected value");
       .([], .([]).().Percent, "The real part of your returned complex number is not sufficiently close to the expected value");
      if (Math.Abs(expected[1]) <= 1) Assert.That(actual[1], Is.EqualTo(expected[1]).Within(1e-12), "The imaginary part of your returned complex number is not sufficiently close to the expected value");
      else Assert.That(actual[1], Is.EqualTo(expected[1]).Within(1e-10).Percent, "The imaginary part of your returned complex number is not sufficiently close to the expected value");
    }
    [Test]
    public void ExampleTests()
    {
      AssertComplexEquals(new double[] {2.995732273553991, 0}, Complex.Log(new double[] {20, 0}));


最佳答案(多种解法):

点击查看答案

更多关联题目:

csharp基础练习题:Delta Bits【难度:1级】–景越C# 经典编程题库,不同难度C# 练习题,适合自学C# 的新手进阶训练
csharp基础练习题:反转值【难度:0级】–景越C# 经典编程题库,不同难度C# 练习题,适合自学C# 的新手进阶训练

免责申明

本博客所有编程题目及答案均收集自互联网,主要用于供网友学习参考,如有侵犯你的权益请联系管理员及时删除,谢谢
题目收集至https://www.codewars.com/
https://www.codewars.com/kata/computing-the-complex-logarithm-function

你可能感兴趣的:(C#编程训练习题答案)