-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
l3fp: How to get a zero filling rounding result? #1226
Comments
\ExplSyntaxOn
\cs_new:Npn \my_fp_round:nn #1#2
{
\int_compare:nNnTF {#2} = \c_zero_int
{ \fp_eval:n { round(#1, 0) } }
{ \exp_args:Ne \__my_fp_round:nn { \fp_eval:n { round(#1, #2) } } {#2} }
}
\cs_new:Npn \__my_fp_round_decimal:w #1 . #2 \q_stop #3 { #3 {#2} }
\cs_new:Npn \__my_fp_round:nn #1
{
#1
\__my_fp_round_decimal:w #1 \q_stop \__my_fp_round_has_decimal:w
. \q_stop \__my_fp_round_no_decimal:nn
}
\cs_new:Npn \__my_fp_round_has_decimal:w
#1 .\q_stop \__my_fp_round_no_decimal:nn #2
{ \prg_replicate:nn { #2 - \str_count_ignore_spaces:n {#1} } { 0 } }
\cs_new:Npn \__my_fp_round_no_decimal:nn #1#2
{ . \prg_replicate:nn {#2} { 0 } }
\tl_show:x { \my_fp_round:nn { 1.001 } { 0 } }
\tl_show:x { \my_fp_round:nn { 1.001 } { 1 } }
\tl_show:x { \my_fp_round:nn { 1.001 } { 2 } }
\tl_show:x { \my_fp_round:nn { 1.001 } { 3 } }
\tl_show:x { \my_fp_round:nn { 1.001 } { 4 } }
\ExplSyntaxOff |
The trailing zeros are trimmed by Doc for
What's the meaning of "non-significant trailing zeros" here? |
Zeros coming after the last non-zero digit, so |
Do we need any action here? Zero filling is a 'display' thing, I suspect. |
Alternatively, @muzimuzhi fancy writing a PR? ;) |
might be nice to add. While it is mostly about display, given |
Sure: I guess in many ways the question is 'what function names do we want inside the expression' (and here I really do mean function!). I guess |
Or |
|
What exactly would |
|
It is possible (too late?) to change the behavior of all four fp rounding functions so that if one of them is used as the outer-most function in an fp expression, For the 'display' purpose, there're full bunch of format string syntax conventions [1] that can be implemented as for example (in an ideal world) [1] In C language https://en.cppreference.com/w/c/io/fprintf and in Java https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/util/Formatter.html#syntax. Java also has a |
I have an almost finished expl3 implementation of C's printf, which includes the regular printf interface, plus formatting functions for individual numbers like |
@PhelypeOleinik Sounds good but I think padding will still be requested 'directly' in |
@muzimuzhi I don't like that at all: rounding and filling zeros are entirely separate concepts (I split them in |
How about extending the |
@josephwright You're right. When typing my previous comment I had semantic of python's built-in |
“rounding and filling zeros are entirely separate” Thus, for example, the use of zero-padding is not related only to the case of “no rounding/trimming”. However, this issue has by now been reduced to the basic idea of “how to specify that padding should be applied in the typeset out”, rather than any such considerations of “mathematical or computational appropriateness/correctness”. |
The idea of zero-padding and when it is advisable/necessary are not, in all cases, "only a display thing”, but this current Thus, if it is easy to provide this type of display then simply go ahead and do it! |
I mean that one shouldn't use rounding to force zero-filling. In |
Agreed.
Probably. It is certainly different from @davidcarlisle's example of conventions such as presentation of (many) currencies using two digital places.
My main point is: does this even need to be expressed here? |
The internal representation of floating point numbers in |
OK, based on @blefloch's point I'm minded to close here as I don't think we want a specific display function for this, and it therefore falls more in the area of a generic number formatter or passing to dedicated routines elsewhere - thoughts? |
I thought that @davidcarlisle was suggesting that we do in fact need to support padding. What is not so clear is whether this should be provided for the display only. Thus doing nothing in l3fp may be sensible, but there may still be a need to provide this simple formatting convention somewhere. |
@car222222 well yes but I don't think anyone doubts there is a requirement, just a case of where to put it. A light weight fp number formatter that sits in the format alongside fpeval would be a good thing, although it doesn't have to be in the fp module at the expl3 level (although that wouldn't be a bad place, I think) |
OK, so perhaps a version of |
Based on @Skillmon's suggestion, that would perhaps be \ExplSyntaxOn
\cs_new:Npn \fp_eval:nn #1#2
{
\int_compare:nNnTF {#2} = \c_zero_int
{ \fp_eval:n {#1} }
{ \exp_args:Ne \__fp_eval:nn { \fp_eval:n {#1} } {#2} }
}
\cs_new:Npn \__fp_eval:nn #1
{
#1
\__fp_eval_decimal:w #1 \q_stop \__fp_eval_has_decimal:w
. \q_stop \__fp_eval_no_decimal:nn
}
\cs_new:Npn \__fp_eval_decimal:w #1 . #2 \q_stop #3 { #3 {#2} }
\cs_new:Npn \__fp_eval_has_decimal:w
#1 .\q_stop \__fp_eval_no_decimal:nn #2
{ \prg_replicate:nn { \int_max:nn {#2 - \str_count_ignore_spaces:n {#1} } { 0 } } { 0 } }
\cs_new:Npn \__fp_eval_no_decimal:nn #1#2
{ . \prg_replicate:nn {#2} { 0 } }
\tl_show:e { \fp_eval:nn { 1.001 } { 0 } }
\tl_show:e { \fp_eval:nn { 1.001 } { 1 } }
\tl_show:e { \fp_eval:nn { 1.001 } { 2 } }
\tl_show:e { \fp_eval:nn { 1.001 } { 3 } }
\tl_show:e { \fp_eval:nn { 1.001 } { 4 } }
\tl_show:e { \fp_eval:nn { 1.001 } { 5 } }
\ExplSyntaxOff |
@josephwright possibly. it depends how light weight we want light weight formatting to be, rather than an integer another possibility would be to to take a token list similar to (or the same as) a subset of C printf %f or %g format string possibilities https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm so |
(I wonder if this is |
Yes, but I suspect that really then is more of a generic |
Personally, I would find the code more readable if this is not just denoted by an extra argument, but by a command name change, e.g., |
@FrankMittelbach Sure, name is to be settled. The question is whether we want something very lightweight in the fp module, or if it belongs elsewhere, or if we need something more complex. |
Currently trailing zeros are always trimmed. Functions
round
,floor
,ceil
, andtrunc
have the same behavior.(#1186 was caused by this)
The text was updated successfully, but these errors were encountered: