Testing Unity a number to the power...yeah

Hello there, it’s me , The Norwegian guy :slight_smile:

here is my try at this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PowersTest : MonoBehaviour
{
float a, b, c;

// Start is called before the first frame update
void Start()
{
    a = 3.0f; //The number 
    b = 3.0F; //To the power

    c = (float)System.Math.Pow(a, b); //Using Visual Studio System Math Pow function

    Debug.Log("Testing System Pow :" + a.ToString() + " to the " + b.ToString() + " Power : " + c.ToString()); //Printing the result to Debug/Consol Window


    c = (float)Mathf.Pow(a, b); //Using Unity built in Math lib Pow function

    Debug.Log("Testing Unity Pow :" + a.ToString() + " to the " + b.ToString() + " Power : " + c.ToString());
    c = Power(a,b); //Had to try

    Debug.Log("Just testing a loop: " + a.ToString() + " to the " + b.ToString() + " Power : " + c.ToString());
}


float Power(float x, float y)
{
    float res; //result
    int turns; // number of power
    turns = (int)y; //making y float into turns int
    res = x; // just making sure the result are correct, ther is pherhaps another way of doing this

    if (y <= 1) //testing for equal or lover than 1, should be interesting to test without this
    {
        res = x; // setting the res to result
    }
    else
    {
        for (int i = 1; i < turns; i++)  //making a foor loop with the number of power
        {
            res = res * x; //Multiplying res with x
        }
    }

    return res; // returning the result (I hope)
}

}

Btw I have no idea how to use the discussion or diffrent markups in community forums hehe

1 Like

Welcome to the forum! We are happy you are here! We have different sections for each course. We have 4 main courses. Unity, Blender, Unreal and Drawing. This right here is where we have discussions for the other courses.

Privacy & Terms