Mod function bug?

Hello,

Unless I’m missing something, it seems the “mod” function is returning bogus results.
Discovered this when writing a converter for HSV to RGB. Didn’t work with the built in “mod” funciton. I wrote a custom “mod” function for the script and everything was OK.

Hope this helps.

Bruce

Hi Bruce,

What development language are you using? Do you have some sample code we can evaluate?

– Dale

Hello Dale,

I’m using the rhino enabled vb script (am currently coding in the editscript Rhino editor).

Any code with the mod function will do, for example 0.5 Mod 2 should equal 0.5, but Rhino is returning 0.

Option Explicit
'Script written by <insert name>
'Script copyrighted by <insert company name>
'Script version December 7, 2015 10:58:45 PM

Call Main()

Sub Main()
     Call Rhino.Print("0.5 Mod 2 = 0.5, but 0.5 Mod 2 =  " & 0.5 Mod 2 & " using Rhino Script.")
End Sub

Best regards,

Bruce

Hi Bruce,

VBScript’s Mod operator divides two integers and returns the integer remainder.

result = number1 Mod number2

If number1 or number2 are floating point numbers, they are first rounded to integers.

If you want to implement a modulus function that operates on floating point numbers, you’ll have to do the work yourself.

For example:

Function Modulo(a, b)
  Modulo = a - (b * (a \ b))
End Function

MsgBox Modulo(0.5, 2)

You can always use this:

http://4.rhino3d.com/5/rhinoscript/utility_methods/colorhsvtorgb.htm

@Dale, quite interesting to see integer division in vbscript and how this function compares to python using negative numbers :wink:

c,

Hello Dale,

Thank you. I guess I should have read the Mod function description more closely.

Best regards,

Bruce

For future reference, I’ve added a new Rhino.FMod function to the Rhino WIP which divides two floating-point numbers and returns the floating-point remainder. You should see this function in next week’s WIP release.

Hello Dale,

Thank you very kindly.

Best regards,

Bruce