Message Boards Message Boards

2
|
3997 Views
|
6 Replies
|
11 Total Likes
View groups...
Share
Share this post:

Can some one explain this odd behavior?

Posted 9 years ago

Hi everyone, I seem to be getting an odd result and cannot see why, here are the code snippets.

k = FromDigits /@ {"729", "243", "027", "001"}

giving {729, 243, 27, 1} as expected, now

u = N[Log[k]/Log[3], 10]

gives {6.000000000, 5.000000000, 3.000000000, 0} also as expected. however this

FractionalPart[N[Log[k]/Log[3], 10]]

gives {1.000000000, 1.000000000, 1.000000000, 0} not expected. if we use this

FractionalPart[{6.000000000, 5.000000000, 3.000000000, 0}]

gives {0., 0., 0., 0} as expected. even

FractionalPart[u]

gives {1.000000000, 1.000000000, 1.000000000, 0}

Any one see what's wrong here? Thanking you all in advance.

Paul.

POSTED BY: Paul Cleary
6 Replies

In your case Rationalize should do the trick:

Rationalize@N[Log[#]/Log[3], 10] & /@ k

or

Rationalize@u

What also works is Round, e.g.

Round[N[Log[#]/Log[3], 10] & /@ k]

or

Round@u

Cheers,

M.

POSTED BY: Marco Thiel

Hi Paul,

I suppose that this is a numerical effect. The elements of u are not exact numbers. It is easy to see as:

u = N[Log[k]/Log[3], 10] - {6, 5, 3, 0}

gives

{0.*10^-10, 0.*10^-10, 0.*10^-10, 0}

So the first entries are not exactly zero. This leads to the symptom that the small deviation is treated by Mathematica as if it was a small negative number. So that the displayed 6.00000000 you see is actually like a 5.999999999999999999999999.... that means that a tiny little number is subtracted from 6 so that the fractional part is nearly (and up to the precision you use) one.

You see that when you calculate:

FractionalPart[0. - N[Log[k]/Log[3], 10]]

(*{0., 0., 0., 0.}*)

and then (without the "." for the 0)

FractionalPart[0 - N[Log[k]/Log[3], 10]]

{-1.000000000, -1.000000000, -1.000000000, 0}

Another way of looking at your problem is to directly use the Logarithm to the right basis:

FractionalPart[N[Log[3, k], 10]]

which gives numerical zeros:

{0.*10^-10, 0.*10^-10, 0.*10^-10, 0}

I don't think that I explained this very well, but I hope you understand what I wanted to say.

Cheers,

M.

POSTED BY: Marco Thiel
Posted 9 years ago

Thank you very much for your quick replies, I do indeed see why I am getting the unexpected results, which now leaves me to ask this question. The line

u = N[Log[k]/Log[3], 10]

giving {6.000000000, 5.000000000, 3.000000000, 0} is clearly indicating that the numbers are 3 to some exact power, as in 3^6, 3^5, 3^3 and 3^0, so how would I extract the integer part not knowing it is an integer as this would be a test of many values?

Paul.

POSTED BY: Paul Cleary

If you look at u you'll see that it is not what you see

u = N[Log[k]/Log[3], 10];
InputForm[u]
(*{5.99999999999999999999999999999999999976`10., 4.99999999999999999999999999999999999979`10., 
 2.9999999999999999999999999999999999999`10., 0}*)

So the fractional part is

FractionalPart[u];
InputForm[%]
(*{0.99999999999999999999999999999999999976`9.221848749616358, 
 0.99999999999999999999999999999999999979`9.301029995663983, 
 0.9999999999999999999999999999999999999`9.52287874528034, 0}*)

What you saw on the screen is

   FractionalPart[u]
   (* {1.000000000, 1.000000000, 1.000000000, 0}*)

You can now hit Return in the output cell to see the actual value. Like this

enter image description here

When you typed FractionalPart[{6.000000000, 5.000000000, 3.000000000, 0}] you actually had 6.000000 as input, and not 5.9999999 as before, and that is why FractionalPart returned zero in this case. The inputs are different, hence the output is different.

POSTED BY: Nasser M. Abbasi
Posted 9 years ago

Hi Marco, thanks again for your reply much appreciated, I had resorted to using Round in the end and Ceiling also worked, what didn't work consistently was IntegerPart out of the 4 values, 3 would be ok and one of them not and then sometimes all 4 would be ok, that I found to be very disconcerting that it wasn't consistent. Anyway I have now utilized the Rationalize function and all is working perfectly now, thank you.

Paul.

POSTED BY: Paul Cleary

Sorry for double posting. I was typing in my reply before seeing yours!

Cheers,

M.

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract