Share via

Power function in Microsoft.CSharpScript.CodeAnalysis C#

Hemanth B 886 Reputation points
2022-01-14T07:46:12.627+00:00

Is there any way where I can evaluate for example 4^6 in Microsoft.CSharpScript.CodeAnalysis without using Math.Pow?

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author
  1. Jack J Jun 25,306 Reputation points
    2022-01-17T02:19:57.387+00:00

    @Hemanth B , based on my research, it is hard to find a method to replace the Math.Pow function because there is no power operator in C#.

    Currently, I only could use the following code to get the result for "4^6".

     static async Task<double> EvaluateFormulaAsync(string formula)  
            {  
                return await CSharpScript.EvaluateAsync<double>(formula, ScriptOptions.Default.WithImports("System.Math"));  
            }  
            static void Main(string[] args)  
            {  
                var result = EvaluateFormulaAsync("System.Math.Pow(4, 6)").Result;  
                Console.WriteLine(result);  
            }  
    

    Result:

    165387-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.