Test of integers? Or, round the number if the first two decimal numbers are sufficiently close to 0 or 1?

Clash Royale CLAN TAG#URR8PPP
The following is a MWE, which explains my intention.
documentclassarticle
usepackagetikz
usetikzlibrarymath
begindocument
tikzmath
integer = 4/2; decimal = 5/3;
integerB=1/3*3;
$integer$ is an integer, and it should be printed as 2.
And $decimal$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
enddocument
I wonder if it is possible to test a number to determine it is an integer. Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1.
tables tikzmath decimal-number
add a comment |
The following is a MWE, which explains my intention.
documentclassarticle
usepackagetikz
usetikzlibrarymath
begindocument
tikzmath
integer = 4/2; decimal = 5/3;
integerB=1/3*3;
$integer$ is an integer, and it should be printed as 2.
And $decimal$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
enddocument
I wonder if it is possible to test a number to determine it is an integer. Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1.
tables tikzmath decimal-number
add a comment |
The following is a MWE, which explains my intention.
documentclassarticle
usepackagetikz
usetikzlibrarymath
begindocument
tikzmath
integer = 4/2; decimal = 5/3;
integerB=1/3*3;
$integer$ is an integer, and it should be printed as 2.
And $decimal$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
enddocument
I wonder if it is possible to test a number to determine it is an integer. Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1.
tables tikzmath decimal-number
The following is a MWE, which explains my intention.
documentclassarticle
usepackagetikz
usetikzlibrarymath
begindocument
tikzmath
integer = 4/2; decimal = 5/3;
integerB=1/3*3;
$integer$ is an integer, and it should be printed as 2.
And $decimal$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
enddocument
I wonder if it is possible to test a number to determine it is an integer. Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1.
tables tikzmath decimal-number
tables tikzmath decimal-number
edited Dec 31 '18 at 16:11
egreg
712k8618913175
712k8618913175
asked Dec 31 '18 at 15:58
GlennGlenn
411
411
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
One cannot say from its floating point representation whether the output of an arithmetic operation involving division or non rational operations is actually an integer.
You can consider the l3fp module of expl3, available through the package xfp.
documentclassarticle
usepackagexfp
begindocument
$fpeval5/3$ is a decimal number, I would like to round it to
$fpevalround(5/3,1)$ or to $fpevalround(5/3,2)$
Another difficulty is that $fpeval(1/3)*3$ is an integer in fact,
and should be printed as $fpevalround((1/3)*3,1)$ or
$fpevalround((1/3)*3,2)$.
enddocument

add a comment |
If the accuracy of your numbers is important you might consider farming that out to a computer algebra system (CAS). The sagetex package relies on the CAS Sage; the documentation can be found on CTAN right here. Documentation on Sage is found here .Sage is not part of the LaTeX distribution (it's big) so it needs to be installed on your computer or, even easier, accessed through a free Cocalc account.
documentclassarticle
usepackagesagetex
usepackagetikz
usetikzlibrarymath
begindocument
beginsagesilent
a = 4/2
b = 5/3
c = 1/3*3
endsagesilent
$sagea$ is an integer, and it should be printed as $sagea$.
And $sageb$ is not an integer. As a decimal it is approximately
$sageb.n(digits=6)$. I would like to round it to $sageb.n(digits=1)$.
Another difficulty is that $sagec$ is an integer in fact,
and should be printed as $sagec$.
enddocument
The output, running in Cocalc, gives:
Notice that Sage interprets your numbers correctly: 4/2 is recognized as 2 and 1/3*3 is recognized as 1. It does need to know the format you want of non integers; but it recognizes that 5/3 is a fraction that can't be reduced and leaves it as a fraction. To force it into a decimal and to specify the number of digits we append .n(digits=6); the documentation is here.
add a comment |
Assuming that this is a question on how to do this with tikzmath, I'd do
documentclassarticle
usepackagetikz
usetikzlibrarymath
begindocument
tikzmath
function myint(x)
if abs(x-round(x)) < 0.1 then printx is an integernewline;
return int(round(x));
else printx is not an integernewline;
return x;
; ;
integer = myint(4/2);
decimal = myint(5/3);
integerB= myint(1/3*3);
$integer$ is an integer, and it should be printed as 2.
And $pgfmathprintnumber[precision=1]decimal$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
enddocument

Note that I used pgfmathprintnumber to round to one digit after the dot. Of course, you can remove the prints.
add a comment |

documentclassarticle
usepackagexintexpr% recommended package
newcommandtest[1]xintifboolexprifint(#1, 1, 0)
xinttheexpr reduce(#1)relaxspace (originally textttdetokenize#1)
is an integer.
xinttheiexpr[2] #1relaxspace (originally textttdetokenize#1),
we rounded to two decimal places) is not an integer.par
begindocument
test4/2
test5/3
test(1/3)*3
test(1/7 - 1/8 - 1/57)*3192
test(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806
enddocument

strangely xintexpr hasifint(expression, YES, NO)function but notisint(expression)which would evaluate to 1 or 0 directly.
– jfbu
Jan 1 at 13:21
about "Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1." this can be done by xintexpr of course as it computes exactly with arbitrarily big fractions. But I don't know exactly what is asked here.
– jfbu
Jan 1 at 13:23
for comparisonfpeval(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806evaluates to0.9999211586333241, exactly likexintthefloatexpr...relax. But the latter afterxintDigits:=48;will evaluate to1.00000000000000000000000000000000000522205263811.
– jfbu
Jan 1 at 13:46
FWIW, using the default precision settings, Lua evaluates(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806as1.000285363444. Of course,roundcomp(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*106500569508061(whereroundandcompare defined in my answer) produces1. Whew!
– Mico
Jan 1 at 14:08
add a comment |
Here's a LuaLaTeX-based solution. integer (4/2) and integerB ((1/3)*3) evaluate to integers automatically according to Lua rules. The LaTeX macro round, which takes two arguments, lets users round numbers to a specified set of digits after the decimal marker.

% !TEX TS-program = lualatex
documentclassarticle
% Set up 2 auxilliary Lua functions to round numbers
directlua
function math.round_int ( x )
return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5)
end
function math.round ( x , n )
return ( math.round_int ( x*10^n ) / 10^n )
end
newcommandcomp[1]directluatex.sprint(#1)
newcommandround[2]directluatex.sprint(math.round(#1,#2))
newcommandintegercomp4/2
newcommanddecimalcomp5/3
newcommandintegerBcomp(1/3)*3
begindocument
integer is an integer.
integerB is also an integer.
decimal is a decimal number. I would like to round it to rounddecimal1.
enddocument
1
does(1/7-1/8-1/57)*3192evaluate to an integer in Lua? (just curious...)
– jfbu
Jan 1 at 13:37
1
in case answer is yes (perhaps from some distributivity done automatically), I have(1/7 - 1/8 - 1/57)*(3000+192)up my sleeve :)
– jfbu
Jan 1 at 13:43
@jfbu - Both expressions evaluate to0.99999999999998(13 nines followed by an 8), using the standard number of significant digits used intex.sprint. If I reduced that number by 1 or 2 digits, one is back to1(pun intended).
– Mico
Jan 1 at 13:46
2
I thinkxintthefloatexprworks withxintDigits:=2;but I am not sure with only 1 digit :) you are really quite a challenge!
– jfbu
Jan 1 at 13:53
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "85"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f468046%2ftest-of-integers-or-round-the-number-if-the-first-two-decimal-numbers-are-suff%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
One cannot say from its floating point representation whether the output of an arithmetic operation involving division or non rational operations is actually an integer.
You can consider the l3fp module of expl3, available through the package xfp.
documentclassarticle
usepackagexfp
begindocument
$fpeval5/3$ is a decimal number, I would like to round it to
$fpevalround(5/3,1)$ or to $fpevalround(5/3,2)$
Another difficulty is that $fpeval(1/3)*3$ is an integer in fact,
and should be printed as $fpevalround((1/3)*3,1)$ or
$fpevalround((1/3)*3,2)$.
enddocument

add a comment |
One cannot say from its floating point representation whether the output of an arithmetic operation involving division or non rational operations is actually an integer.
You can consider the l3fp module of expl3, available through the package xfp.
documentclassarticle
usepackagexfp
begindocument
$fpeval5/3$ is a decimal number, I would like to round it to
$fpevalround(5/3,1)$ or to $fpevalround(5/3,2)$
Another difficulty is that $fpeval(1/3)*3$ is an integer in fact,
and should be printed as $fpevalround((1/3)*3,1)$ or
$fpevalround((1/3)*3,2)$.
enddocument

add a comment |
One cannot say from its floating point representation whether the output of an arithmetic operation involving division or non rational operations is actually an integer.
You can consider the l3fp module of expl3, available through the package xfp.
documentclassarticle
usepackagexfp
begindocument
$fpeval5/3$ is a decimal number, I would like to round it to
$fpevalround(5/3,1)$ or to $fpevalround(5/3,2)$
Another difficulty is that $fpeval(1/3)*3$ is an integer in fact,
and should be printed as $fpevalround((1/3)*3,1)$ or
$fpevalround((1/3)*3,2)$.
enddocument

One cannot say from its floating point representation whether the output of an arithmetic operation involving division or non rational operations is actually an integer.
You can consider the l3fp module of expl3, available through the package xfp.
documentclassarticle
usepackagexfp
begindocument
$fpeval5/3$ is a decimal number, I would like to round it to
$fpevalround(5/3,1)$ or to $fpevalround(5/3,2)$
Another difficulty is that $fpeval(1/3)*3$ is an integer in fact,
and should be printed as $fpevalround((1/3)*3,1)$ or
$fpevalround((1/3)*3,2)$.
enddocument

answered Dec 31 '18 at 16:10
egregegreg
712k8618913175
712k8618913175
add a comment |
add a comment |
If the accuracy of your numbers is important you might consider farming that out to a computer algebra system (CAS). The sagetex package relies on the CAS Sage; the documentation can be found on CTAN right here. Documentation on Sage is found here .Sage is not part of the LaTeX distribution (it's big) so it needs to be installed on your computer or, even easier, accessed through a free Cocalc account.
documentclassarticle
usepackagesagetex
usepackagetikz
usetikzlibrarymath
begindocument
beginsagesilent
a = 4/2
b = 5/3
c = 1/3*3
endsagesilent
$sagea$ is an integer, and it should be printed as $sagea$.
And $sageb$ is not an integer. As a decimal it is approximately
$sageb.n(digits=6)$. I would like to round it to $sageb.n(digits=1)$.
Another difficulty is that $sagec$ is an integer in fact,
and should be printed as $sagec$.
enddocument
The output, running in Cocalc, gives:
Notice that Sage interprets your numbers correctly: 4/2 is recognized as 2 and 1/3*3 is recognized as 1. It does need to know the format you want of non integers; but it recognizes that 5/3 is a fraction that can't be reduced and leaves it as a fraction. To force it into a decimal and to specify the number of digits we append .n(digits=6); the documentation is here.
add a comment |
If the accuracy of your numbers is important you might consider farming that out to a computer algebra system (CAS). The sagetex package relies on the CAS Sage; the documentation can be found on CTAN right here. Documentation on Sage is found here .Sage is not part of the LaTeX distribution (it's big) so it needs to be installed on your computer or, even easier, accessed through a free Cocalc account.
documentclassarticle
usepackagesagetex
usepackagetikz
usetikzlibrarymath
begindocument
beginsagesilent
a = 4/2
b = 5/3
c = 1/3*3
endsagesilent
$sagea$ is an integer, and it should be printed as $sagea$.
And $sageb$ is not an integer. As a decimal it is approximately
$sageb.n(digits=6)$. I would like to round it to $sageb.n(digits=1)$.
Another difficulty is that $sagec$ is an integer in fact,
and should be printed as $sagec$.
enddocument
The output, running in Cocalc, gives:
Notice that Sage interprets your numbers correctly: 4/2 is recognized as 2 and 1/3*3 is recognized as 1. It does need to know the format you want of non integers; but it recognizes that 5/3 is a fraction that can't be reduced and leaves it as a fraction. To force it into a decimal and to specify the number of digits we append .n(digits=6); the documentation is here.
add a comment |
If the accuracy of your numbers is important you might consider farming that out to a computer algebra system (CAS). The sagetex package relies on the CAS Sage; the documentation can be found on CTAN right here. Documentation on Sage is found here .Sage is not part of the LaTeX distribution (it's big) so it needs to be installed on your computer or, even easier, accessed through a free Cocalc account.
documentclassarticle
usepackagesagetex
usepackagetikz
usetikzlibrarymath
begindocument
beginsagesilent
a = 4/2
b = 5/3
c = 1/3*3
endsagesilent
$sagea$ is an integer, and it should be printed as $sagea$.
And $sageb$ is not an integer. As a decimal it is approximately
$sageb.n(digits=6)$. I would like to round it to $sageb.n(digits=1)$.
Another difficulty is that $sagec$ is an integer in fact,
and should be printed as $sagec$.
enddocument
The output, running in Cocalc, gives:
Notice that Sage interprets your numbers correctly: 4/2 is recognized as 2 and 1/3*3 is recognized as 1. It does need to know the format you want of non integers; but it recognizes that 5/3 is a fraction that can't be reduced and leaves it as a fraction. To force it into a decimal and to specify the number of digits we append .n(digits=6); the documentation is here.
If the accuracy of your numbers is important you might consider farming that out to a computer algebra system (CAS). The sagetex package relies on the CAS Sage; the documentation can be found on CTAN right here. Documentation on Sage is found here .Sage is not part of the LaTeX distribution (it's big) so it needs to be installed on your computer or, even easier, accessed through a free Cocalc account.
documentclassarticle
usepackagesagetex
usepackagetikz
usetikzlibrarymath
begindocument
beginsagesilent
a = 4/2
b = 5/3
c = 1/3*3
endsagesilent
$sagea$ is an integer, and it should be printed as $sagea$.
And $sageb$ is not an integer. As a decimal it is approximately
$sageb.n(digits=6)$. I would like to round it to $sageb.n(digits=1)$.
Another difficulty is that $sagec$ is an integer in fact,
and should be printed as $sagec$.
enddocument
The output, running in Cocalc, gives:
Notice that Sage interprets your numbers correctly: 4/2 is recognized as 2 and 1/3*3 is recognized as 1. It does need to know the format you want of non integers; but it recognizes that 5/3 is a fraction that can't be reduced and leaves it as a fraction. To force it into a decimal and to specify the number of digits we append .n(digits=6); the documentation is here.
answered Dec 31 '18 at 16:41
DJPDJP
7,10421630
7,10421630
add a comment |
add a comment |
Assuming that this is a question on how to do this with tikzmath, I'd do
documentclassarticle
usepackagetikz
usetikzlibrarymath
begindocument
tikzmath
function myint(x)
if abs(x-round(x)) < 0.1 then printx is an integernewline;
return int(round(x));
else printx is not an integernewline;
return x;
; ;
integer = myint(4/2);
decimal = myint(5/3);
integerB= myint(1/3*3);
$integer$ is an integer, and it should be printed as 2.
And $pgfmathprintnumber[precision=1]decimal$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
enddocument

Note that I used pgfmathprintnumber to round to one digit after the dot. Of course, you can remove the prints.
add a comment |
Assuming that this is a question on how to do this with tikzmath, I'd do
documentclassarticle
usepackagetikz
usetikzlibrarymath
begindocument
tikzmath
function myint(x)
if abs(x-round(x)) < 0.1 then printx is an integernewline;
return int(round(x));
else printx is not an integernewline;
return x;
; ;
integer = myint(4/2);
decimal = myint(5/3);
integerB= myint(1/3*3);
$integer$ is an integer, and it should be printed as 2.
And $pgfmathprintnumber[precision=1]decimal$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
enddocument

Note that I used pgfmathprintnumber to round to one digit after the dot. Of course, you can remove the prints.
add a comment |
Assuming that this is a question on how to do this with tikzmath, I'd do
documentclassarticle
usepackagetikz
usetikzlibrarymath
begindocument
tikzmath
function myint(x)
if abs(x-round(x)) < 0.1 then printx is an integernewline;
return int(round(x));
else printx is not an integernewline;
return x;
; ;
integer = myint(4/2);
decimal = myint(5/3);
integerB= myint(1/3*3);
$integer$ is an integer, and it should be printed as 2.
And $pgfmathprintnumber[precision=1]decimal$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
enddocument

Note that I used pgfmathprintnumber to round to one digit after the dot. Of course, you can remove the prints.
Assuming that this is a question on how to do this with tikzmath, I'd do
documentclassarticle
usepackagetikz
usetikzlibrarymath
begindocument
tikzmath
function myint(x)
if abs(x-round(x)) < 0.1 then printx is an integernewline;
return int(round(x));
else printx is not an integernewline;
return x;
; ;
integer = myint(4/2);
decimal = myint(5/3);
integerB= myint(1/3*3);
$integer$ is an integer, and it should be printed as 2.
And $pgfmathprintnumber[precision=1]decimal$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
enddocument

Note that I used pgfmathprintnumber to round to one digit after the dot. Of course, you can remove the prints.
edited Dec 31 '18 at 18:20
answered Dec 31 '18 at 17:50
marmotmarmot
91.1k4105198
91.1k4105198
add a comment |
add a comment |

documentclassarticle
usepackagexintexpr% recommended package
newcommandtest[1]xintifboolexprifint(#1, 1, 0)
xinttheexpr reduce(#1)relaxspace (originally textttdetokenize#1)
is an integer.
xinttheiexpr[2] #1relaxspace (originally textttdetokenize#1),
we rounded to two decimal places) is not an integer.par
begindocument
test4/2
test5/3
test(1/3)*3
test(1/7 - 1/8 - 1/57)*3192
test(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806
enddocument

strangely xintexpr hasifint(expression, YES, NO)function but notisint(expression)which would evaluate to 1 or 0 directly.
– jfbu
Jan 1 at 13:21
about "Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1." this can be done by xintexpr of course as it computes exactly with arbitrarily big fractions. But I don't know exactly what is asked here.
– jfbu
Jan 1 at 13:23
for comparisonfpeval(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806evaluates to0.9999211586333241, exactly likexintthefloatexpr...relax. But the latter afterxintDigits:=48;will evaluate to1.00000000000000000000000000000000000522205263811.
– jfbu
Jan 1 at 13:46
FWIW, using the default precision settings, Lua evaluates(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806as1.000285363444. Of course,roundcomp(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*106500569508061(whereroundandcompare defined in my answer) produces1. Whew!
– Mico
Jan 1 at 14:08
add a comment |

documentclassarticle
usepackagexintexpr% recommended package
newcommandtest[1]xintifboolexprifint(#1, 1, 0)
xinttheexpr reduce(#1)relaxspace (originally textttdetokenize#1)
is an integer.
xinttheiexpr[2] #1relaxspace (originally textttdetokenize#1),
we rounded to two decimal places) is not an integer.par
begindocument
test4/2
test5/3
test(1/3)*3
test(1/7 - 1/8 - 1/57)*3192
test(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806
enddocument

strangely xintexpr hasifint(expression, YES, NO)function but notisint(expression)which would evaluate to 1 or 0 directly.
– jfbu
Jan 1 at 13:21
about "Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1." this can be done by xintexpr of course as it computes exactly with arbitrarily big fractions. But I don't know exactly what is asked here.
– jfbu
Jan 1 at 13:23
for comparisonfpeval(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806evaluates to0.9999211586333241, exactly likexintthefloatexpr...relax. But the latter afterxintDigits:=48;will evaluate to1.00000000000000000000000000000000000522205263811.
– jfbu
Jan 1 at 13:46
FWIW, using the default precision settings, Lua evaluates(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806as1.000285363444. Of course,roundcomp(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*106500569508061(whereroundandcompare defined in my answer) produces1. Whew!
– Mico
Jan 1 at 14:08
add a comment |

documentclassarticle
usepackagexintexpr% recommended package
newcommandtest[1]xintifboolexprifint(#1, 1, 0)
xinttheexpr reduce(#1)relaxspace (originally textttdetokenize#1)
is an integer.
xinttheiexpr[2] #1relaxspace (originally textttdetokenize#1),
we rounded to two decimal places) is not an integer.par
begindocument
test4/2
test5/3
test(1/3)*3
test(1/7 - 1/8 - 1/57)*3192
test(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806
enddocument


documentclassarticle
usepackagexintexpr% recommended package
newcommandtest[1]xintifboolexprifint(#1, 1, 0)
xinttheexpr reduce(#1)relaxspace (originally textttdetokenize#1)
is an integer.
xinttheiexpr[2] #1relaxspace (originally textttdetokenize#1),
we rounded to two decimal places) is not an integer.par
begindocument
test4/2
test5/3
test(1/3)*3
test(1/7 - 1/8 - 1/57)*3192
test(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806
enddocument

edited Jan 1 at 13:40
answered Jan 1 at 13:20
jfbujfbu
46.4k66148
46.4k66148
strangely xintexpr hasifint(expression, YES, NO)function but notisint(expression)which would evaluate to 1 or 0 directly.
– jfbu
Jan 1 at 13:21
about "Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1." this can be done by xintexpr of course as it computes exactly with arbitrarily big fractions. But I don't know exactly what is asked here.
– jfbu
Jan 1 at 13:23
for comparisonfpeval(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806evaluates to0.9999211586333241, exactly likexintthefloatexpr...relax. But the latter afterxintDigits:=48;will evaluate to1.00000000000000000000000000000000000522205263811.
– jfbu
Jan 1 at 13:46
FWIW, using the default precision settings, Lua evaluates(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806as1.000285363444. Of course,roundcomp(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*106500569508061(whereroundandcompare defined in my answer) produces1. Whew!
– Mico
Jan 1 at 14:08
add a comment |
strangely xintexpr hasifint(expression, YES, NO)function but notisint(expression)which would evaluate to 1 or 0 directly.
– jfbu
Jan 1 at 13:21
about "Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1." this can be done by xintexpr of course as it computes exactly with arbitrarily big fractions. But I don't know exactly what is asked here.
– jfbu
Jan 1 at 13:23
for comparisonfpeval(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806evaluates to0.9999211586333241, exactly likexintthefloatexpr...relax. But the latter afterxintDigits:=48;will evaluate to1.00000000000000000000000000000000000522205263811.
– jfbu
Jan 1 at 13:46
FWIW, using the default precision settings, Lua evaluates(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806as1.000285363444. Of course,roundcomp(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*106500569508061(whereroundandcompare defined in my answer) produces1. Whew!
– Mico
Jan 1 at 14:08
strangely xintexpr has
ifint(expression, YES, NO) function but not isint(expression) which would evaluate to 1 or 0 directly.– jfbu
Jan 1 at 13:21
strangely xintexpr has
ifint(expression, YES, NO) function but not isint(expression) which would evaluate to 1 or 0 directly.– jfbu
Jan 1 at 13:21
about "Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1." this can be done by xintexpr of course as it computes exactly with arbitrarily big fractions. But I don't know exactly what is asked here.
– jfbu
Jan 1 at 13:23
about "Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1." this can be done by xintexpr of course as it computes exactly with arbitrarily big fractions. But I don't know exactly what is asked here.
– jfbu
Jan 1 at 13:23
for comparison
fpeval(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806 evaluates to 0.9999211586333241, exactly like xintthefloatexpr...relax. But the latter after xintDigits:=48; will evaluate to 1.00000000000000000000000000000000000522205263811.– jfbu
Jan 1 at 13:46
for comparison
fpeval(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806 evaluates to 0.9999211586333241, exactly like xintthefloatexpr...relax. But the latter after xintDigits:=48; will evaluate to 1.00000000000000000000000000000000000522205263811.– jfbu
Jan 1 at 13:46
FWIW, using the default precision settings, Lua evaluates
(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806 as 1.000285363444. Of course, roundcomp(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*106500569508061 (where round and comp are defined in my answer) produces 1. Whew!– Mico
Jan 1 at 14:08
FWIW, using the default precision settings, Lua evaluates
(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806 as 1.000285363444. Of course, roundcomp(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*106500569508061 (where round and comp are defined in my answer) produces 1. Whew!– Mico
Jan 1 at 14:08
add a comment |
Here's a LuaLaTeX-based solution. integer (4/2) and integerB ((1/3)*3) evaluate to integers automatically according to Lua rules. The LaTeX macro round, which takes two arguments, lets users round numbers to a specified set of digits after the decimal marker.

% !TEX TS-program = lualatex
documentclassarticle
% Set up 2 auxilliary Lua functions to round numbers
directlua
function math.round_int ( x )
return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5)
end
function math.round ( x , n )
return ( math.round_int ( x*10^n ) / 10^n )
end
newcommandcomp[1]directluatex.sprint(#1)
newcommandround[2]directluatex.sprint(math.round(#1,#2))
newcommandintegercomp4/2
newcommanddecimalcomp5/3
newcommandintegerBcomp(1/3)*3
begindocument
integer is an integer.
integerB is also an integer.
decimal is a decimal number. I would like to round it to rounddecimal1.
enddocument
1
does(1/7-1/8-1/57)*3192evaluate to an integer in Lua? (just curious...)
– jfbu
Jan 1 at 13:37
1
in case answer is yes (perhaps from some distributivity done automatically), I have(1/7 - 1/8 - 1/57)*(3000+192)up my sleeve :)
– jfbu
Jan 1 at 13:43
@jfbu - Both expressions evaluate to0.99999999999998(13 nines followed by an 8), using the standard number of significant digits used intex.sprint. If I reduced that number by 1 or 2 digits, one is back to1(pun intended).
– Mico
Jan 1 at 13:46
2
I thinkxintthefloatexprworks withxintDigits:=2;but I am not sure with only 1 digit :) you are really quite a challenge!
– jfbu
Jan 1 at 13:53
add a comment |
Here's a LuaLaTeX-based solution. integer (4/2) and integerB ((1/3)*3) evaluate to integers automatically according to Lua rules. The LaTeX macro round, which takes two arguments, lets users round numbers to a specified set of digits after the decimal marker.

% !TEX TS-program = lualatex
documentclassarticle
% Set up 2 auxilliary Lua functions to round numbers
directlua
function math.round_int ( x )
return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5)
end
function math.round ( x , n )
return ( math.round_int ( x*10^n ) / 10^n )
end
newcommandcomp[1]directluatex.sprint(#1)
newcommandround[2]directluatex.sprint(math.round(#1,#2))
newcommandintegercomp4/2
newcommanddecimalcomp5/3
newcommandintegerBcomp(1/3)*3
begindocument
integer is an integer.
integerB is also an integer.
decimal is a decimal number. I would like to round it to rounddecimal1.
enddocument
1
does(1/7-1/8-1/57)*3192evaluate to an integer in Lua? (just curious...)
– jfbu
Jan 1 at 13:37
1
in case answer is yes (perhaps from some distributivity done automatically), I have(1/7 - 1/8 - 1/57)*(3000+192)up my sleeve :)
– jfbu
Jan 1 at 13:43
@jfbu - Both expressions evaluate to0.99999999999998(13 nines followed by an 8), using the standard number of significant digits used intex.sprint. If I reduced that number by 1 or 2 digits, one is back to1(pun intended).
– Mico
Jan 1 at 13:46
2
I thinkxintthefloatexprworks withxintDigits:=2;but I am not sure with only 1 digit :) you are really quite a challenge!
– jfbu
Jan 1 at 13:53
add a comment |
Here's a LuaLaTeX-based solution. integer (4/2) and integerB ((1/3)*3) evaluate to integers automatically according to Lua rules. The LaTeX macro round, which takes two arguments, lets users round numbers to a specified set of digits after the decimal marker.

% !TEX TS-program = lualatex
documentclassarticle
% Set up 2 auxilliary Lua functions to round numbers
directlua
function math.round_int ( x )
return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5)
end
function math.round ( x , n )
return ( math.round_int ( x*10^n ) / 10^n )
end
newcommandcomp[1]directluatex.sprint(#1)
newcommandround[2]directluatex.sprint(math.round(#1,#2))
newcommandintegercomp4/2
newcommanddecimalcomp5/3
newcommandintegerBcomp(1/3)*3
begindocument
integer is an integer.
integerB is also an integer.
decimal is a decimal number. I would like to round it to rounddecimal1.
enddocument
Here's a LuaLaTeX-based solution. integer (4/2) and integerB ((1/3)*3) evaluate to integers automatically according to Lua rules. The LaTeX macro round, which takes two arguments, lets users round numbers to a specified set of digits after the decimal marker.

% !TEX TS-program = lualatex
documentclassarticle
% Set up 2 auxilliary Lua functions to round numbers
directlua
function math.round_int ( x )
return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5)
end
function math.round ( x , n )
return ( math.round_int ( x*10^n ) / 10^n )
end
newcommandcomp[1]directluatex.sprint(#1)
newcommandround[2]directluatex.sprint(math.round(#1,#2))
newcommandintegercomp4/2
newcommanddecimalcomp5/3
newcommandintegerBcomp(1/3)*3
begindocument
integer is an integer.
integerB is also an integer.
decimal is a decimal number. I would like to round it to rounddecimal1.
enddocument
answered Jan 1 at 12:59
MicoMico
275k30372759
275k30372759
1
does(1/7-1/8-1/57)*3192evaluate to an integer in Lua? (just curious...)
– jfbu
Jan 1 at 13:37
1
in case answer is yes (perhaps from some distributivity done automatically), I have(1/7 - 1/8 - 1/57)*(3000+192)up my sleeve :)
– jfbu
Jan 1 at 13:43
@jfbu - Both expressions evaluate to0.99999999999998(13 nines followed by an 8), using the standard number of significant digits used intex.sprint. If I reduced that number by 1 or 2 digits, one is back to1(pun intended).
– Mico
Jan 1 at 13:46
2
I thinkxintthefloatexprworks withxintDigits:=2;but I am not sure with only 1 digit :) you are really quite a challenge!
– jfbu
Jan 1 at 13:53
add a comment |
1
does(1/7-1/8-1/57)*3192evaluate to an integer in Lua? (just curious...)
– jfbu
Jan 1 at 13:37
1
in case answer is yes (perhaps from some distributivity done automatically), I have(1/7 - 1/8 - 1/57)*(3000+192)up my sleeve :)
– jfbu
Jan 1 at 13:43
@jfbu - Both expressions evaluate to0.99999999999998(13 nines followed by an 8), using the standard number of significant digits used intex.sprint. If I reduced that number by 1 or 2 digits, one is back to1(pun intended).
– Mico
Jan 1 at 13:46
2
I thinkxintthefloatexprworks withxintDigits:=2;but I am not sure with only 1 digit :) you are really quite a challenge!
– jfbu
Jan 1 at 13:53
1
1
does
(1/7-1/8-1/57)*3192 evaluate to an integer in Lua? (just curious...)– jfbu
Jan 1 at 13:37
does
(1/7-1/8-1/57)*3192 evaluate to an integer in Lua? (just curious...)– jfbu
Jan 1 at 13:37
1
1
in case answer is yes (perhaps from some distributivity done automatically), I have
(1/7 - 1/8 - 1/57)*(3000+192) up my sleeve :)– jfbu
Jan 1 at 13:43
in case answer is yes (perhaps from some distributivity done automatically), I have
(1/7 - 1/8 - 1/57)*(3000+192) up my sleeve :)– jfbu
Jan 1 at 13:43
@jfbu - Both expressions evaluate to
0.99999999999998 (13 nines followed by an 8), using the standard number of significant digits used in tex.sprint. If I reduced that number by 1 or 2 digits, one is back to 1 (pun intended).– Mico
Jan 1 at 13:46
@jfbu - Both expressions evaluate to
0.99999999999998 (13 nines followed by an 8), using the standard number of significant digits used in tex.sprint. If I reduced that number by 1 or 2 digits, one is back to 1 (pun intended).– Mico
Jan 1 at 13:46
2
2
I think
xintthefloatexpr works with xintDigits:=2; but I am not sure with only 1 digit :) you are really quite a challenge!– jfbu
Jan 1 at 13:53
I think
xintthefloatexpr works with xintDigits:=2; but I am not sure with only 1 digit :) you are really quite a challenge!– jfbu
Jan 1 at 13:53
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f468046%2ftest-of-integers-or-round-the-number-if-the-first-two-decimal-numbers-are-suff%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown