Group Abstract Group Abstract

Message Boards Message Boards

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

Can some one explain this odd behavior?

Posted 11 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
Posted 11 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

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
Posted 11 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

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

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 BY: Nasser M. Abbasi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard