WikiProject iconMathematics Template‑class
WikiProject iconThis module is within the scope of WikiProject Mathematics, a collaborative effort to improve the coverage of mathematics on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
TemplateThis module does not require a rating on Wikipedia's content assessment scale.

This module implements a collection of related mathematical operations and number formatting tasks.

bug "�" edit

Hello, this is not enwiki problem. On bnwiki, this module sometime gives incorrect result. There is problem with "precision_format". For example, {{#invoke:Math|precision_format| 2.0004 | 3}} gives ২�000 instead of ২,০০০ (more example, bn:Module:Math, sandbox). How can i fix this? Please feel free to edit sandbox. --আফতাবুজ্জামান (talk) 23:56, 2 June 2020 (UTC)Reply

@আফতাবুজ্জামান: That bug is due to the following optimistic code:
local zero_sep = lang:formatNum(1.1)
formatted_num = formatted_num .. zero_sep:sub(2,2)
The code is trying to determine the character for the decimal point (for example, . or ,). The code assumes the local number uses English digits in which case :sub(2,2) will extract the second character which they hope is . or ,. However, at bnwiki 1.1 is ১.১ and the code gets the second byte of the first digit which gives that broken Unicode symbol. A simple fix would be to change the above by commenting out or deleting the first line, and editing the second line:
--local zero_sep = lang:formatNum(1.1)
formatted_num = formatted_num .. '.'
A bit more clever would be to rearrange it to use mw.ustring.sub but that's pointless for you because you know you want a dot. The result is going to use en digits. There's not much that can be done about that apart from the conversion code that we have discussed in the past. Johnuniq (talk) 05:09, 3 June 2020 (UTC)Reply
@Johnuniq: Thanks. Problem resolved. Everything is giving correct result but for some reason {{round|0.000020004|7}} is giving incorrect result. আফতাবুজ্জামান (talk) 03:02, 4 June 2020 (UTC)Reply

@আফতাবুজ্জামান:Hmm, that seems to be a bug in lang:formatNum. Put the following in a module sandbox and test with {{#invoke:SANDBOXNAME|main}}.

local function main()
    local lang = mw.getContentLanguage()
    local r = lang:formatNum(0.00002)
    return #r .. ' /' .. r .. '/'
end
return {main=main}

Trying this gives the results shown:

  • At bnwiki: 7 /২.০/ (in en digits, that's "7 /2.0/")
  • At enwiki: 6 /2.0E-5/

The 7 is the length (number of bytes) in the result. Each of the two bn digits is 3 bytes in UTF-8 and the dot is 1, so they add up to 7. The point of that is that there is nothing in the output that is hidden—it's only 7 bytes. I guess you could ask about that at Phabricator. Perhaps e notation is not supported in a language where it is not usually used? Is it used? Johnuniq (talk) 03:59, 4 June 2020 (UTC)Reply

@Johnuniq: Ya, e notation is uncommon in Bengali. I'm not sure what should i request when submitting a bug for this. If you can, please do. Anyway, after spending many hours i was able to do this, it seems this fixes the problem. Should i implement? --আফতাবুজ্জামান (talk) 00:44, 7 June 2020 (UTC)Reply
@আফতাবুজ্জামান: That change looks good, well done! I examined the code as far as your change goes but deciding that the overall function always works would be mind-spinnnig stuff which is why I didn't dive in earlier. Re my earlier comment about a bug, if you link to a module sandbox I could create (it would be Module:Sandbox/Johnuniq/format here) I'll probably try reporting it. Johnuniq (talk) 01:29, 7 June 2020 (UTC)Reply
@Johnuniq: Thanks. here. আফতাবুজ্জামান (talk) 01:45, 7 June 2020 (UTC)Reply
@আফতাবুজ্জামান: I posted at phab:T254683. Johnuniq (talk) 07:45, 7 June 2020 (UTC)Reply

I see that a software update since the above has changed what occurs. bn:Module talk:খেলাঘর/Johnuniq/format used to show that lang:formatNum(0.00002) gave the bn equivalent of "2.0" (wrong) while here at enwiki it gave "2.0E-5". Now, both results are the equivalent of "0". Some consequences:

  • {{#invoke:Math|precision_format|2.0004|3}} → 2.000 (good)
  • {{round|0.00019|4}} → 0.0002 (good)
  • {{round|0.000019|5}} → 0
  • {{round|0.000020004|7}} → 000

Johnuniq (talk) 06:26, 11 January 2021 (UTC)Reply

Protected edit request on 10 March 2021 edit

Please copy from Module:Math/sandbox to implement the merge of Module:PassMath per Wikipedia:Templates for discussion/Log/2021 February 28#Module:PassMath * Pppery * it has begun... 20:14, 10 March 2021 (UTC)Reply

  Done Primefac (talk) 18:09, 11 March 2021 (UTC)Reply
Note that I made a typo in a comment when implementing this merge. It's probably not worth making an edit to a module this highly used to correct it, but I've fixed it in the sandbox. * Pppery * it has begun... 20:34, 11 March 2021 (UTC)Reply
Given that it's instructions (though I will say buried outside of the /doc) it could potentially cause issues, so I've updated it. Primefac (talk) 22:24, 11 March 2021 (UTC)Reply

lang:formatNum fix for small numbers of order -5 and smaller edit

lang:formatNum for numbers of order -5 and smaller returns 0 (for en language). Temporary(?) fix in lua pseudocode:

if order(math.abs(value)) < -4
  formatted_num = lang:formatNum(math.abs(value), noCommafy=true)
else
  formatted_num = lang:formatNum(math.abs(value))

Context: Template_talk:Round#Bug_in_rounding_0.000020004?. MarMi wiki (talk) 19:49, 26 April 2021 (UTC)Reply

Implemented in sandbox. MarMi wiki (talk) 15:21, 27 April 2021 (UTC)Reply

Rounding in the module and rounding in the expr edit

p._round function.

For 0.005 it returns: invoke 0.01, expr 0.01

For -0.005 it returns: invoke 0.00, expr -0.01

Called by: invoke:Math|precision_format|0.005|2, expr:0.005 round 2

What type of rounding function exactly it is supposed to be? It looks like half up rounding, which doesn't match the rounding used by expr. Is that intentional? MarMi wiki (talk) 19:10, 27 August 2023 (UTC)Reply