Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

If I have a fractional value, how can I truncate (not round) that to a certain number of bits, like 8?

For example, for Pi 3.141569265359... the fractional part is 0.141569265359...

NumberForm[BaseForm[FractionalPart[N[Pi]],2],8]

0.00100100001111110110101010001

But this is giving me 8 decimal digits of PI in base 2. What I want is 8 binary digits in base 2.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

NumberForm is for representation purposes only. I suggest you use RealDigits, and then take the number of bits you need from its first returned element or, better yet

Rest[ First[ RealDigits[FractionalPart[N[Pi]], 2, 8+1]]]

EDIT: corrected to give only the bits needed.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.