Jump to content

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


 Share

Recommended Posts

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

Link to comment
Share on other sites

  • Developers

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

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

Link to comment
Share on other sites

  • Developers

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

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

@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

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...