Ask Your Question
1

Display decimal as a fraction?

asked 2013-06-30 13:32:27 -0500

bxdin gravatar image

updated 2016-03-08 14:01:33 -0500

FrédéricC gravatar image

The number 1.5 represents 3/2 in decimal form.

In sage I can get 3/2 to display as a decimal: 3/2.n()

I haven't had any luck converting 1.5 to a fraction.

edit retag flag offensive close merge delete

3 answers

Sort by » oldest newest most voted
5

answered 2013-06-30 13:36:27 -0500

tmonteil gravatar image

updated 2013-06-30 13:44:19 -0500

You can try:

sage: QQ(1.5)
3/2

or:

sage: (1.5).exact_rational()
3/2

As a warning, i let you undertand the following behaviour with decimal numbers:

sage: (1.1).exact_rational()
2476979795053773/2251799813685248
edit flag offensive delete link more

Comments

Thank you. But what about numbers such as 4/3, that outputs .3333 to an infinity?

bxdin gravatar imagebxdin ( 2013-06-30 13:51:05 -0500 )edit

You will get: sage: RR(4/3) 1.33333333333333 sage: QQ(RR(4/3)) 4/3 sage: RR(4/3).exact_rational() 6004799503160661/4503599627370496

tmonteil gravatar imagetmonteil ( 2013-06-30 13:55:59 -0500 )edit

So there's no way to convert/simplify, 1.33333333333333, to 4/3 instead of that last humongous number?

bxdin gravatar imagebxdin ( 2013-06-30 14:17:05 -0500 )edit
2

Nope. It is related to precision problems. See this for example: sage: 0.1 + 0.1 + 0.1 == 0.3 False

ppurka gravatar imageppurka ( 2013-07-01 05:52:11 -0500 )edit
3

Indeed. 4/3 is not a floating-point number since it can not be written as `p/2^q`. So, when you write sage: a = RR(4/3) You only get an approximation of `4/3`, whose value is sage: a.exact_rational() 6004799503160661/4503599627370496 If you want to recover `4/3` from `a`, you can do sage: a.simplest_rational() 4/3 or, sage: QQ(a) 4/3 But you should understand that two roundings were done during your computation, and that you may have lost something there.

tmonteil gravatar imagetmonteil ( 2013-07-01 12:31:19 -0500 )edit
1

answered 2013-09-15 09:18:19 -0500

dazedANDconfused gravatar image

I ran across another way that's posted here where the author, John D Cook mentions the nsimplify command in SymPy (which you can can access in Sage). As the post indicates, you can suggest constants (eg pi) that should be considered and "...can also give nsimplify a tolerance, asking it to find a simple representation within a neighborhood of the number". Here's a screenshot of it recognizing 1.3333333333333 in a Sage Cell Server. image description

edit flag offensive delete link more
0

answered 2016-02-29 18:59:46 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

1.5 equals to 1 1/2 .

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-06-30 13:32:27 -0500

Seen: 7,824 times

Last updated: Sep 15 '13