Sign in to follow this  
Followers 0
Mat

$ans = 1, but ($ans = 1) = False

12 posts in this topic

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:

Posted Image

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

Share this post


Link to post
Share on other sites



Flootingpoint calculation. try

$ans = Int((root ($a + ($b * sqrt ($c)), 3)) + (root ($a - ($b * sqrt ($c)), 3)))


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Sorry... but thats wrong too :)

Int rounds (or negates any decimals), so int(1.1) = 1

That means I end up way off...

MDiesel

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

?? 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

Share this post


Link to post
Share on other sites

#8 ·  Posted (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.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

:) I understand but you only have 15 decimal places to play with. doesn't rounding on 14 decimals work for you? Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

@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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0