Mat Posted July 18, 2009 Share Posted July 18, 2009 I decided to jump into the deep end on project euler, and try 251. For those interested, a brute force method should work, just very slowly. A triplet of positive integers (a,b,c) is called a Cardano Triplet if it satisfies the condition: 2, 1, 5 was the example they gave, yet I wasn't able to reproduce it using my formula. After some checking, it turns out the formula was returning 1, but when asked whether it = 1, it returns false. script:$a = 2 $b = 1 $c = 5 $ans = 0 $ans = ((root ($a + ($b * sqrt ($c)), 3)) + (root ($a - ($b * sqrt ($c)), 3))) MsgBox (0, $ans = 1, $ans) Func Root($fNum, $nExp = 3) Local $bNeg = False, $fRet = 0 If $nExp < 0 Then Return SetError (1, 0, $fNum) If $fNum < 0 Then ; is negative If Mod($nExp, 2) Then ; nExp is odd, so negative IS possible. $bNeg = True $fNum *= -1 Else Return SetError(1, 0, $fNum & "i") ; Imaginary number. EndIf EndIf $fRet = $fNum ^ (1 / $nExp) If $bNeg Then $fRet *= -1 Return $fRet EndFunc ; ==> Root$ans is shown as 1$ans = 1 is false!!Any ideas? as it is starting to bug me.MDiesel AutoIt Project Listing Link to comment Share on other sites More sharing options...
Developers Jos Posted July 18, 2009 Developers Share Posted July 18, 2009 Flootingpoint calculation. try $ans = Int((root ($a + ($b * sqrt ($c)), 3)) + (root ($a - ($b * sqrt ($c)), 3))) SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Mat Posted July 18, 2009 Author Share Posted July 18, 2009 ah! I knew floating point numbers did provide some hassle for us, but I was not aware that it would affect this, thanks jos! Should have just about done it then!!! MDiesel AutoIt Project Listing Link to comment Share on other sites More sharing options...
Mat Posted July 18, 2009 Author Share Posted July 18, 2009 Sorry... but thats wrong too Int rounds (or negates any decimals), so int(1.1) = 1 That means I end up way off... MDiesel AutoIt Project Listing Link to comment Share on other sites More sharing options...
Mat Posted July 18, 2009 Author Share Posted July 18, 2009 I can't edit the last post Solution I think: $ans = int (((root ($a + ($b * sqrt ($c)), 3)) + (root ($a - ($b * sqrt ($c)), 3))) * 10000) If $ans = 10000 Then MsgBox (0, "", "") I can also edit the accuracy... MDiesel AutoIt Project Listing Link to comment Share on other sites More sharing options...
Developers Jos Posted July 18, 2009 Developers Share Posted July 18, 2009 Have you tried using the round() function to limit the number of decimal places? SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Mat Posted July 18, 2009 Author Share Posted July 18, 2009 ?? The problem is the rounding !!! I don't want to limit the number of decimal places! The thing is... Int rounds. I need it to equal exactly 1, not 1.1 My method does NOT work incidentally, i am trying to find out why. All I need is a way to accuratetll tell if floating point number X == 1. It seems to be pretty annoying... There must be some way... MDiesel AutoIt Project Listing Link to comment Share on other sites More sharing options...
Developers Jos Posted July 18, 2009 Developers Share Posted July 18, 2009 (edited) ?? The problem is the rounding !!! I don't want to limit the number of decimal places!The thing is... Int rounds. I need it to equal exactly 1, not 1.1My method does NOT work incidentally, i am trying to find out why. All I need is a way to accuratetll tell if floating point number X == 1.It seems to be pretty annoying...There must be some way...MDiesel I understand but you only have 15 decimal places to play with. doesn't rounding on 14 decimals work for you? Edited July 18, 2009 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
trancexx Posted July 18, 2009 Share Posted July 18, 2009 @mdiesel; Jos is right of course. It's a matter of how your processor (or any) interprets floating-point numbers. Floating-point number is 32 bits in length (as int is). So all of it needs to be within those freaking 32 bits.This means that two different numbers (in nature - and not only two) are likely to be represented as the same floats by the machine.This is the cause of wrong results.This also means that you need to calculate with divergence. All this to say that rounding is essential when evaluating mathematical operations with floating-point numbers done by machine. Avoiding that is wrong. ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
Mat Posted July 19, 2009 Author Share Posted July 19, 2009 So your saying Round (14) would do it? at a friends house atm, so ill check later. but right now i'm not exactly thinking strait, mdiesel AutoIt Project Listing Link to comment Share on other sites More sharing options...
Xand3r Posted July 19, 2009 Share Posted July 19, 2009 here's an easy fix for that MsgBox (0, String($ans) = 1 , String($ans)) Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro Link to comment Share on other sites More sharing options...
Mat Posted July 19, 2009 Author Share Posted July 19, 2009 here's an easy fix for that MsgBox (0, String($ans) = 1 , String($ans)) hmm... Thats a good idea too... I'm trying everything out at the moment. MDiesel AutoIt Project Listing Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now