Home > front end >  How to use Math.Pow properly to avoid getting "There is no argument given corresponds to the re
How to use Math.Pow properly to avoid getting "There is no argument given corresponds to the re

Time:10-23

I am getting this error in my code: There is no argument given corresponds to the required formal pattern 'y' of Math.Pow(double, double).

using System;
using System.ComponentModel.DataAnnotations;

namespace AssignmentTwo.Models
{
    public class FutureValueCalculator
    {

        [Required(ErrorMessage = "Please enter a Principal value.")]
        [Range(50000, 1000000, ErrorMessage =
               "Principle value must be between 50,000 to 10,00,000.")]
        public decimal? PrincipleValue { get; set; }

        [Required(ErrorMessage = "Please enter a yearly interest rate.")]
        [Range(0.1, 10.0, ErrorMessage =
               "Yearly interest rate must be between 0.1 and 10.0.")]
        public decimal? YearlyInterestRate { get; set; }

        [Required(ErrorMessage = "Please enter Compounding periods per year.")]
        [Range(1, 24, ErrorMessage =
               "Compounding period must be between 1 to 24 according to months")]
        public decimal? CompoundingPeriod { get; set; }

        [Required(ErrorMessage = "Please enter a number of years.")]
        [Range(1, 50, ErrorMessage =
               "Number of years must be between 1 and 50.")]
        public int? Years { get; set; }

        public decimal? CalculateFutureValue()
        {
            int? months = Years * 12;
            decimal? monthlyInterestRate = YearlyInterestRate / 12 / 100;
            
            decimal? futureValue = 0;
            for (int i = 0; i < months; i  )
            {
                futureValue = Decimal.ToDouble(Math.Pow((PrincipleValue * (1   (YearlyInterestRate / CompoundingPeriod)), CompoundingPeriod * Years)));
            }
            return futureValue;
        }

        }
    }

CodePudding user response:

Your paranthesis are not aligned. Notice that Pow method expects 2 double inputs whereas your params were decimal.

Also you are trying to convert nullable decimals to double (you may also get exception when dividing with 0) I would recommend improve your code with those validations first, but if you are just targeting to make that code work, below should work.

Also edit your question as well. Check this to learn more about asking.

 public decimal? CalculateFutureValue(int? Years, decimal? YearlyInterestRate, decimal? PrincipleValue,
                                         decimal? CompoundingPeriod)
    {
        int? months = Years * 12;
        decimal? monthlyInterestRate = YearlyInterestRate / 12 / 100;
    
        decimal? futureValue = 0;
        for (int i = 0; i < months; i  ) {
            futureValue =
                Convert.ToDecimal(
                Math.Pow(
                    Decimal.ToDouble(PrincipleValue.Value * (1   (YearlyInterestRate.Value / CompoundingPeriod.Value))),
                    Decimal.ToDouble(CompoundingPeriod.Value * Years.Value)
                    )
            );
        }
    
        return futureValue;
    }

That is a pretty fragile code.. Again, I would recommend make those input params either not nullable, or at least have some sort of null check before putting them into calculation.

CodePudding user response:

The problem is in this Math.Pow((PrincipleValue * (1 (YearlyInterestRate / CompoundingPeriod)), CompoundingPeriod * Years)) line, it should be like this.

Math.Pow((PrincipleValue * (1   (YearlyInterestRate / CompoundingPeriod)), CompoundingPeriod * Years), <somthingShouldBePasswedHere>)

Because of Math.Pow requires two parameters and you are passing a single parameter in your case.

  •  Tags:  
  • c#
  • Related