On 01/17/2012 05:18 AM, Kevin Fishburne wrote: > On 01/16/2012 02:33 AM, Benoît Minisini wrote: >> Le 16/01/2012 08:05, Kevin Fishburne a écrit : >>> On 01/16/2012 01:43 AM, Kevin Fishburne wrote: >>>> Why would I get the runtime error "Mathematic error" in this function: >>>> >>>> Public Function Distance(x1 As Single, y1 As Single, x2 As Single, y2 As >>>> Single) As Single >>>> Return Abs(Sqr((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))) >>>> End >>>> >>>> The values of the variables are: >>>> >>>> ?x2& ""& x1& ""& x2& ""& x1& ""& y2& >>>> ""& y1& ""& y2& >>>> ""& y1 >>>> >>>> 174.248779296875 2.40799128109576E-41 174.248779296875 >>>> 2.40799128109576E-41 146.759170532227 7.19552647834628E+27 >>>> 146.759170532227 7.19552647834628E+27 >>>> >>>> Using GAMBAS 3, revision unknown. I'm recompiling now to see if it makes >>>> a difference, but please let me know what this error means and what >>>> could cause it. >>>> >>> I changed the first line (old one) to the second: >>> >>> 'Return Abs(Sqr((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))) >>> Return Sqr((x2 - x1) ^ 2 + (y2 - y1) ^ 2) >>> >>> and it gives me no problems. Seems like I really screwed up the distance >>> calculation, but even so this may be a bug so hopefully I've contributed >>> to a solution. Still curious of course as to what the error might mean. >>> Maybe it should just say "You suck at math." :) >>> >> You get a mathematic error because of an overflow. (y2 - y1) * (y2 - y1) >> cannot be expressed by a Single in your example. >> >> The "^" operator deals with Float internally, so you don't get an error. >> >> Anyway, to compute a distance, you can use the Hyp() or the Mag() >> function. It will be faster. > Sometimes it calculates without error, other times it doesn't. Changing > all the datatypes to float doesn't make any difference either. Thanks > for the efficiency tip on Hyp() and Mag(), but it still looks like > there's a problem with math here. How should a manual distance > calculation be expressed without overflow if that's the problem? >
Just discovered it has to do with one of the passed values being zero. Why is that? -- Kevin Fishburne Eight Virtues www: http://sales.eightvirtues.com e-mail: [email protected] phone: (770) 853-6271 ------------------------------------------------------------------------------ Keep Your Developer Skills Current with LearnDevNow! The most comprehensive online learning library for Microsoft developers is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, Metro Style Apps, more. Free future releases when you subscribe now! http://p.sf.net/sfu/learndevnow-d2d _______________________________________________ Gambas-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/gambas-user
