Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Changing values error with parameters

edited June 2012 in Questions Posts: 12

I am making an algebric calculator but at this moment the main thing I would like to make before finishing it is to find a way of changing the values of the variables during the simulation, and i cant do it with parameters (nor iparameters), I always get an error saying it cant perform arithmetics on the value. Help please?

Comments

  • edited June 2012 Posts: 50

    Please paste your code, then we might be able to help you.

  • edited June 2012 Posts: 12
    A = 1
    B = 2
    C = 3
    function Normal:init(x)
        d = B * B - 4 * A * C
        print("A = "..A)
        print("B = "..B)
        print("C = "..C)
        print("-----------------------")
        print("Delta = "..d)
        x1 = (-B + math.sqrt(d)) / 2 * A
        x2 = (-B - math.sqrt(d)) / 2 * A
        print("X1 = "..x1)
        print("X2 = "..x2)
    end
    

    what i want is a way to change A,B and C while in the simulation, parameters dont work whenever i try it says the parameter is a nil value.
    I tried using:

    A = parameter("A",-100,100)
    
    parameter("A",-100,100)
    
    iparameter("A",-100,0,100)
    

    and

    A = iparameter("A",-100,0,100)
    
  • Posts: 80

    This works, don't know if it helps...


    parameter("A",-100,100,100) B = 2 C = 3 function calc()     clearOutput()     d = B * B - 4 * A * C     print("A = "..A)     print("B = "..B)     print("C = "..C)     print("-----------------------")     print("Delta = "..d)     x1 = (-B + math.sqrt(d)) / 2 * A     x2 = (-B - math.sqrt(d)) / 2 * A     print("X1 = "..x1)     print("X2 = "..x2) end function draw()     calc()      end
  • Posts: 12

    Thank You! It is just what I wanted! :-D

  • Posts: 12

    There is a way of not selecting decimals on the parameters? Because it is quite annoying :(

  • Posts: 80

    Maybe you could make a

    math.round
    

    At the end of the calc function to snap it to the integers or so?

  • edited June 2012 Posts: 12

    I got 2 problems: when i put math.round at the end of function calc() it says "= expected
    near end" and i got multiple calculations in this program, and in the biquadratic equation class it dont recognizes x1. Here is the whole thing:

    Biquadrada = class()
    parameter("A",-100,100,100)
    parameter("B",-100,100,100)
    parameter("C",-100,100,100)
    function Biquadrada:init(x)
        end
        function calc()
        d = B * B - 4 * A * C
        m1 = (-B + math.sqrt(d)) / 2 * A
        m2 = (-B - math.sqrt(d)) / 2 * A
        x1 = math.sqrt(m1)
        x2 = - math.sqrt(m1)
        x3 = math.sqrt(m2)
        x4 = -math.sqrt(m2)
        if x2 == -0 then
            x2 = x2 * -1
            elseif x4 == -0 then
                x4 = x4 * - 1
          math.round      
        end
    end
    
    function Biquadrada:draw()
        calc()
        background(0, 0, 0, 255)
        pushStyle()
        fill(255, 0, 0, 255)
        font("AmericanTypewriter-Light")
        fontSize(60)
        textAlign(LEFT)
        text("A = "..A,WIDTH/2,HEIGHT -60)
        text("B = "..B,WIDTH/2,HEIGHT -130)
        text("C = "..C,WIDTH/2,HEIGHT -200)
        text("Delta = "..d,WIDTH/2,HEIGHT -270)
        text("X1 = "..x1,WIDTH/2,HEIGHT -340)
        text("X2 = "..x2,WIDTH/2,HEIGHT -410)
        text("X3 = "..x3,WIDTH/2,HEIGHT -480)
        text("X4 = "..x4,WIDTH/2,HEIGHT -550)
        fill(190, 190, 190, 66)
        font("Baskerville-Italic")
        fontSize(105)
        text("ax^4 + bx^2 + c",WIDTH/2,HEIGHT -670)
    end
    

    (im Brazilian BTW so biquadrada = biquadratic)

  • SimeonSimeon Admin Mod
    edited June 2012 Posts: 5,054

    @PLPP math.round is a function, so you have to give it a number as a parameter. For example:

    x = 5.6
    math.round( x )
    
    -- Will return 6
    

    There is also math.floor (always round down) and math.ceil (always round up).

  • Posts: 12

    No such luck

    parameter("A",-100,100,0)
    math.floor( A )
    
  • SimeonSimeon Admin Mod
    Posts: 5,054

    You are not using the result of math.floor( A )

    E.g.

    B = math.floor( A )
    print( B )
    
  • Posts: 85

    I'm happy to see that there is a "round" function in the "math" library, because I wasn't able to find it in the manual. Perhaps it should be there...

  • Posts: 179

    I didn't know there was one. I have been spinning my own.

    function math.round(num)
        return math.floor(num + 0.5)
    end
    

    I guess I can stop using that code.

  • Posts: 12

    Still not working...

    parameter("A",-100,100,0)
    a2 = math.floor( A )
    
  • Posts: 563

    I'm pretty sure there isn't a math.round in Codea - however @Vega's solution works fine.

  • SimeonSimeon Admin Mod
    Posts: 5,054

    Ah you're right, @Reefwing. I just assumed it was there.

Sign In or Register to comment.