creatures caves welcome, guest
downloads   gallery   dev   community   creatchi   forum   mycaves
bookmarks | search | post new topic
Development Forum
old
Maths and Variables?   
Malkin

Malkin
Australia  
Manager


 visit Malkin's website: Malkin's page at CWiki
  12/4/2013

How do you express some basic mathematics in CAOS?

Let OV01 equal a random number between 100 and 2000

setv ov01 rand 100 2000


How would you express setting

OV02 = (OV01+1)?

or OV03 = (OV01*2)?

I see that there is a SQRT command in the caos listing, but no square command?


My TCR Norns
 
Ghosthande
Prodigal Sock

Ghosthande


 visit Ghosthande's website: Breeders Beware
  12/4/2013

ADDV lets you add things.
MULV lets you multiply things.

I don't think you can do what you're asking with only one line. CAOS just doesn't allow for very complicated statements. You tend to end up with stuff like this:

setv va00 ov01
addv va00 1
doif ov02 eq va00
etc.

This is partially why the method for increasing an agent's pose number is so obnoxious:

setv va00 pose
addv va00 1
pose va00

OFC, if there's a more efficient way to do this kind of math, I'd be happy to be proven wrong. But I don't think there is. :\



 
Malkin

Malkin

Manager


 visit Malkin's website: Malkin's page at CWiki
  12/4/2013

So, following on from that, you'd be able to set OV02 to (OV01-1) by doing the following:

setv va00 ov01
subv va00 1
setv ov02 eq va00


and setting OV03 to (OV01*2) would be

setv va01 ov01
mulv va01 2
setv ov03 eq va01


or, for fun, dividing ov03 by 3:

setv va02 ov03
divv va02 3
setv ov04 eq va02


When working with mulv and divv, do they round to full numbers, or do they keep going on? Could I end up with a variable that is set to 3.6 repeata?


My TCR Norns
 
Moe

Moe


 visit Moe's website: Creatures 2 to Docking Station
  12/4/2013  1

It depends how you have defined the variable. CAOS can be a bit tricky in this regard since it doesn't, on the surface, have clear numeric variable types.

There are 2 types of numbers the engine considers: Integer, and Float.

Integers are 1, 2, 3, 4, 5, etc. Think: Whole Numbers

Float numbers (Floating Point) include decimals. 1.1, 2.56, 3.14, etc. Think: Fractions in decimal format.

Integers are the engine's default variable assignment, and it will work ONLY within the confines of integers until you specify more detail. If you write:

SETV va00 10
DIVV va00 7

10 divided by 7, you will only get the value 1 in response, despite the real value being 1.428...etc. It is not working with decimal places. It truncates them.

However, if you set the value to 10.00, the engine now knows that you have specified a floating point variable since you've included decimals places in it's assignment.

SETV va00 10.00
DIVV va00 7

Here you will get the decimal places out to 6 digits: 1.428571

The opposite is also true. If you had set va00 to 10 and divided by 7.00, the engine will know that the output needs to be a floating point number and will give you the 1.428571 value.


For the sake of memory space, the engine will not store more than a certain number of decimal places. It will truncate after about 6 decimal places.

So, 3.1415926535 becomes 3.141592.

So, you cannot have 1/3rd come out to .333 repeating indefinitely. Only 0.333333 will be stored.


Fun Fact: the RAND command, used for generating a random number between a specified range will not use Float numbers or generate decimals. So Rand 120.7 to 128.9 will only specify numbers like 122, 126, etc. You can cheat the engine though by randomizing 10 times what you need and dividing by 100.00 later to get a floating point value.

Example: I need a random number between 0 and 10, including some decimals.

setv va00 0 (0 x 100 = 0.00)
setv va01 1000 (10 x 100=1000.00)
setv va02 rand va00 va01
divv va02 100.00

This will output numbers for you anywhere in the scale up to 2 decimal places. 3.54, 2.87, 9.95, etc. :)

Edit: Simplified code:
setv va00 rand 0 1000
divv va00 100.00


 
Malkin

Malkin

Manager


 visit Malkin's website: Malkin's page at CWiki
  12/4/2013

Wow, interesting stuff!

So, in this example, because I'm pegging everything off this RAND OV01, if I use DIVV later on, I'll get a whole number, because I've set the OV to use an integer. Even if it should really be 3.6 repeata, the engine would round up to 4?

When would you want to get a decimal number? Have you ever used decimals in your coding?


My TCR Norns
 
Ghosthande
Prodigal Sock

Ghosthande


 visit Ghosthande's website: Breeders Beware
  12/4/2013

Ooh, that's neat to know, Moe.

Explains some funky stuff that happened a few years back, too. I seem to remember a flying agent that had the "disappearing decimals" issue, but that was before I was familiar with floats so I was completely mystified at the time. Mystery solved. :P



 
Moe

Moe


 visit Moe's website: Creatures 2 to Docking Station
  12/4/2013  1

You'll get a whole number, unless you DIVV by a value with a decimal, then the result is converted to a Float.

SETV Ov01 rand 0 1000
^ Is our starting line

Let's say that the random number it chose is 19. It is a whole number, an integer.

If we:
DIVV Ov01 5 (Divide 19 by 5)

The engine will give us 3, because we divided by an integer.

If we:
DIVV Ov01 5.00 (Divide 19 by 5.00)

The engine will give us 3.8 (the true answer), because since we added the ".00" it knows that the answer should be a floating point.

Now you notice it did not round up to give us 4 in the first example. The engine doesn't round, it simply cuts off anything after the decimal.


To answer your question, I have used decimals in my coding! I use the random method I detailed above to give more variety to agents that "spray" things, like seed launchers or explosions. Otherwise you get very hard intervals and set patterns, especially with velocities which rarely exceed 10 in any direction.

You also can't accurately work with CA values without using floating point, since all CA values are less than or equal to 1.000000.

Admittedly, the instances are far and few between, but every now and then I realize that an error in one of my agent's behavior is because I was using integers when I should have been using floats.

It's just something to watch out for.


While on the topic of RAND, here is another neat tidbit:

RAND can be used implicitly in a DOIF statement. It does not need to be assigned first. Some developers, including CL coders, set a variable to be RAND X Y, and then run a doif block using that:

SETV Va00 RAND 0 100
Doif va00 eq X
[Etc]
Endi

When in fact, you can use the RAND code right in the DOIF statement:

Doif RAND 0 100 eq X
[Etc]
Endi

 
Malkin

Malkin

Manager


 visit Malkin's website: Malkin's page at CWiki
  12/7/2013

When the divisor is a variable (and integer), could that truncation create a situation when you are attempting to divide by 0?

That situation, when you're choosing the rand directly in the doif statement, does that work with all of the doif 'condition words'? (AND, OR, EQ, NE, GT, GE, LT, LE.)


doif rand 0 100 le 50 and rand 10 20 eq 15
etc. etc.
endi


My TCR Norns
 
Malkin

Malkin

Manager


 visit Malkin's website: Malkin's page at CWiki
  12/13/2013

Is it a bad idea for this kind of maths to be done INSTantaneously?

My TCR Norns
 
Papriko
Peppery One

Papriko



  12/13/2013

INST is never a bad idea. INST prevents interruptions. The only way INST could be considered "bad" is that all commands have to be done at once, costing quite some computing power. Except for that, it only has advantages to use INST.

Lets play plants! Photosynthesis... Photosynthesis... Photosynthesis...
 
Moe

Moe


 visit Moe's website: Creatures 2 to Docking Station
  12/13/2013

Speaking to the point of performance, this is why I recommend against INST except when you absolutely need it. Most agents and code blocks do not need to be performed instantaneously to successfully and predictably execute. At such a point, you're basically ONLY using up performance and computational power and gaining no advantage whatsoever.

Only INST code blocks that need INST'ing. One example might be when you're using an ESEE or ENUM block to perform operations on external agents, or if you have a block of code that operates on an existing saved agent. You will have errors if that agent disappears while you're trying to operate on it, so INST'ing that block within an eclipsed "doif targ ne null" frame *might* be necessary (there are cases when even that isn't needed!).

It's an old engine that isn't keeping up with the advances in hardware, so use INST sparingly, and always remember to use a SLOW when you're done to let the engine know it can resume it's normal pace of execution.


As an example from experience, if I used INST on all the timer scripts on all the agents in C2toDS, performance would more than half, it would downright plummet. I have also written code blocks that, when not INST'ed, run fine, but when INST'ed, caused the engine to actually hiccup (on an eight core 4.6Ghz computer). Moral of the story, INST is only advantageous when it's needed. Otherwise it's a bane of performance. Use it with wisdom and prudence.

 
Ghosthande
Prodigal Sock

Ghosthande


 visit Ghosthande's website: Breeders Beware
  12/13/2013

^ Definitely. I see people use INST for things where timing isn't an issue, just because they don't want their code interrupted. But that's why we have LOCK and UNLK--which do exactly what it sounds like and don't (to my knowledge) pose any kind of extra drain on memory.


 
Malkin

Malkin

Manager


 visit Malkin's website: Malkin's page at CWiki
  12/13/2013

So I would set my OVs as 0 when making the agent (INSTantaneously), and then perhaps use the timer script or the creation script to do the maths to set the OVs to what I want them to be?

My TCR Norns
 
Moe

Moe


 visit Moe's website: Creatures 2 to Docking Station
  12/13/2013

If you're only needing the variables set once, you may as well do it in the creation code block, or the Creation Script, and INST isn't an issue. INST really becomes a problem when it's used excessively in repetitious places like the Timer Script.
 
Malkin

Malkin

Manager


 visit Malkin's website: Malkin's page at CWiki
  9/4/2014

Looking at the syntax for ADDV, it seems like I couldn't use it to add GAME variables... is this correct?

My TCR Norns
 
Papriko
Peppery One

Papriko



  9/4/2014

I think it should work fine, as game variables are pretty much just like any other variable. Usually the arguments are passed to the innermost command, so in addv game "MyGameVar" 3 rgam it should be able to realize that "MyGameVar" belongs to the game command, not to the addv.
But to be honest, I got a little rusty in CAOS by now, so the best would be to just go along and try it out yourself. Making a custom game variable really isn't much more than simply using it.

Don't forget to rgam after changes and eventually delg your variable when you are done messing.


Lets play plants! Photosynthesis... Photosynthesis... Photosynthesis...
 


downloads
cobs
adoptions
creaturelink
metarooms
breeds
 
gallery
art
wallpaper
screenshots
graphics
promos
sprites
dev
hack shack
script reservations
dev resources
active projects
dev forum
 
community
links
advice
chat
polls
resources
creatchi
 
forum
bookmarks
general
news
help
development
strangeo
survivor
mycaves
log in
register
lost pw
0 online
creatures caves is your #1 resource for the creatures artificial life game series: creatures, creatures 2, creatures 3, docking station, and the upcoming creatures family.

contact    help    privacy policy    terms & conditions    rules    donate    wiki