Ask Your Question
2

f string float not working

asked 2020-07-22 18:55:11 +0100

cybervigilante gravatar image

updated 2020-07-22 19:13:44 +0100

slelievre gravatar image

Why am I getting this error with a perfectly valid f-string? SageMath is using Python 3.7.3. I've tried variants a dozen times with the same error.

val = 12.3
print(f'{val:.2f}')

TypeError                                 Traceback (most recent call last)
<ipython-input-13-b868ffed9f12> in <module>()
      1 val = RealNumber('12.3')
      2 
----> 3 print(f'{val:.2f}')

TypeError: unsupported format string passed
to sage.rings.real_mpfr.RealLiteral.__format__
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2020-07-22 19:11:21 +0100

slelievre gravatar image

updated 2020-07-22 19:20:10 +0100

This is a known bug, tracked at

Two workarounds exist:

  • either convert to a float:

    sage: val = 12.3
    sage: print(f'{float(val):.2f}')
    
  • or use a raw float from the start

    sage: val = 12.3r
    sage: print(f'{val:.2f}')
    
edit flag offensive delete link more

Comments

Thanks. I'm using Sagemath 9.0 . Would upgrading to 9.1 help or is it still not fixed?

cybervigilante gravatar imagecybervigilante ( 2020-07-22 20:28:05 +0100 )edit

Looks like it still wasn't fixed. Back to forcing a float. Is sagemath choking on f strings in any other way? https://trac.sagemath.org/query?miles...

cybervigilante gravatar imagecybervigilante ( 2020-07-22 20:36:24 +0100 )edit
0

answered 2020-07-22 19:31:44 +0100

Emmanuel Charpentier gravatar image

This is know and a relevant ticket is open.

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

1 follower

Stats

Asked: 2020-07-22 18:55:11 +0100

Seen: 607 times

Last updated: Jul 22 '20