How to do math properly?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
rururubell
Newbie
Posts: 13
Joined: Mon Sep 30, 2019 4:00 am
Deviantart: rururubell
Contact:

How to do math properly?

#1 Post by rururubell »

I have some code on renpy with atl. But it don't works properly. If I use calc and get value of -50.0 + ( 300.0 * math.tan( 32.7 ) ) and put it in renpy - it works well. But I want for renpy to calc it. Sorry for my English.
111.png
And how can I print calc result to console? If I use python.print(...) I get error.
Sorry for my English!

User avatar
gas
Miko-Class Veteran
Posts: 842
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: How to do math properly?

#2 Post by gas »

Be sure to import math.
Use a function that return the value instead of putting the formula directly.

Code: Select all

init python:
    import math
    def uncanny_math(a,b,c):
        return a+ ( b * math.tan( c ) )

###
xpos (whatever) ypos uncanny_math(-50.0, 300.0,  32.7)
As for console outputs:
https://www.renpy.org/doc/html/develope ... oper-tools
Probably is the renpy.log() thing you're searching about.

(you look knowledgeable enough to do this on your own).
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

User avatar
rururubell
Newbie
Posts: 13
Joined: Mon Sep 30, 2019 4:00 am
Deviantart: rururubell
Contact:

Re: How to do math properly?

#3 Post by rururubell »

A try to make a function but this didn't work too. -50 + ( 300 * tan( 32.7) ) should be 142 I'm a right or not? If I put this walue, all works well, but not this code. And if I uncomment renpy.log(...) line, I get a error.

My code:
Screenshot_2020-12-27_17-13-35.png
Sorry for my English!

User avatar
gas
Miko-Class Veteran
Posts: 842
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: How to do math properly?

#4 Post by gas »

Python return a value of 967 for such operation. The math.tan argument must be expressed in radians, so math.tan(32.7) return 3.3915861586637797. Multiply by 300 and subtract 50 ('-50 +' means 'add a negative of 50' so equal subtracting) and you'll go near 1000.

For renpy.log() you should first define the config.log. https://www.renpy.org/doc/html/config.h ... config.log

define config.log = "logtext.txt"

and you should add it to the function, not the transform.

Code: Select all

define config.log = "mylog.txt"
init python:
    def func(x):
        y = x*2
        renpy.log(str(y))
        return y
For such kind of math operations you can simply install python and use the python shell, so you can verify what python return and do all experiments.

(don't use screenshots to report code, there are visually impaired people around. Copy/paste it. One of the best coder I met in my life was a blind guy)
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

User avatar
rururubell
Newbie
Posts: 13
Joined: Mon Sep 30, 2019 4:00 am
Deviantart: rururubell
Contact:

Re: How to do math properly?

#5 Post by rururubell »

gas wrote: Sun Dec 27, 2020 12:02 pm radians
Oh, this was my problem, I forgot about radians. But how to get grads? I use math.degrees() math.radians() but don't get normal results. For example tan in xcalc get me 0.641 and that's value what I need. In python I get:

194.323 if I use math.degrees( math.tan( 32.7 ) )
36.783 if I use math.degrees( math.tan( math.radians( 32.7 ) ) )
Sorry for my English!

User avatar
rururubell
Newbie
Posts: 13
Joined: Mon Sep 30, 2019 4:00 am
Deviantart: rururubell
Contact:

Re: How to do math properly?

#6 Post by rururubell »

Oh, thank's a lot! I get properly value after all, when I use math.tan( math.radians( 32.7 ) ). Now all works well, thanks again!
Sorry for my English!

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]