Cannot Calculate the Modulus of Non Integers?

Post » Tue Jun 19, 2012 2:42 am

I'm having a bit of trouble with a script in my mod. Everything's compiling and working except this one calculation, should be returning the moon phase as a number 0 - 7 (actually as a number between 1 and 24 or something, since it needs to be divided by three and rounded down, but that's a separate part of the script which is working fine.)


 cohMoonPhase.SetValue((GameDaysPassed.Value + ( GameHour.Value / 24 ) - 0.5)% 24 )

That line of code is identical to one found in the Oblivion mod Curse of Hircine, except for the updated syntax, and yet its telling me that you cannot calculate the modulus of a non-integer.

Here's the Curse of Hircine version:


set phaseday to ((GameDaysPassed + (GetCurrentTime/24) - 0.5)%24)



The only problem I can think of is maybe that Oblivion automatically rounded when calculating the modulus of a value, though using the floor() command (with "math" imported) doesn't seem to help, for some reason it returns a number it most certainly shouldn't : 18 on the night of a waning gibbous moon, which corresponds to a waning crescent moon.

I'd appreciate any help anyone who can provide in solving this problem. It's really the last big issue preventing the mod I'm working on from being releasable.
User avatar
Ymani Hood
 
Posts: 3514
Joined: Fri Oct 26, 2007 3:22 am

Post » Tue Jun 19, 2012 2:46 am

You'll probably need to manually cast it as int. You could use an extra variable and the http://www.creationkit.com/Floor_-_Math function to store the data after the decimal point if you want.

Cipscis
User avatar
C.L.U.T.C.H
 
Posts: 3385
Joined: Tue Aug 14, 2007 6:23 pm

Post » Tue Jun 19, 2012 9:50 am

Well, I just got it working: Current line is

  cohMoonPhase.SetValue( ( floor( floor(GameDaysPassed.Value + ( GameHour.Value / 24 ) - 0.5)% 24 ) / 3 ) ) 

Well, its mostly working, to correct that previous statement. The phase changes a day early for some reason I'm not quite sure about.
User avatar
Tiff Clark
 
Posts: 3297
Joined: Wed Aug 09, 2006 2:23 am

Post » Tue Jun 19, 2012 7:36 am

A few things:
- Modulo should only work with integers as floats don't conceptually have a remainder. 4.5 / 3.0 is 1.5, not 1.0 remainder 1.5
- You shouldn't need to round/floor when doing integer division. 23 / 3 = 7 Unless papyrus is doing something weird and converts to floats
- I don't know papyrus, but this is how most languages treat primitive data types.

How does the cycle of the moon phases work? 8 phases with 3 days per phase? Something like this should work:
int isMorn = 0if (GameHour.Value < 12)	isMorn = 1int phase = (GameDaysPassed.Value % 24) - isMornif (phase < 0)	phase += 24phase /= 3cohMoonPhase.SetValue(phase)

isMorn is subtracted after modulo, but can end up with a negative at the beginning of the cycle. the if check just wraps the value back up to 23.
This should work, since the morning of the first day is actually the end of the 8th phase and resets the cycle.
User avatar
Sami Blackburn
 
Posts: 3306
Joined: Tue Jun 20, 2006 7:56 am

Post » Tue Jun 19, 2012 12:04 am

Casting stuff ...

http://www.creationkit.com/Cast_Reference
User avatar
YO MAma
 
Posts: 3321
Joined: Thu Dec 21, 2006 8:24 am


Return to V - Skyrim