最初のページに戻ります。

総合の目次があるページに戻ります。

よく使うマニュアルです

Wiki

updated on 2004.06.23

16.34.MOD()とINT()にご用心

[ Previous ] [ HOME ] [ Upper ] [ Next ]


大したこと無いですが有る程度、英語が分かるので、海外のメーリングリストに流した、質問を翻訳してみます。それから、へたくそな私の英語でも、ちゃんと、世界のプログラマーは答えてくれるんですよ。

事の起こりは、EXCELのMODでした。ここにその時の模様をご報告します。すこし、スレッドが壊れていますけど、ほんの数時間で、この様なアドバイスがもらえるのは、「驚異」であり、「感激」でもあります。

Hi, all
By Excel functions, int(-8.1)=-9. And %Int in RPGiv is -8. How can I get -9 same as an Excel function by RPG iv?
Is which result(-9 or -8) normal for you ?

こんにちは、皆さん。
EXCELの機能では、INT(-8.1)=-9です。そして、RPGiVの%INTでは-8です。どうしたら、RPGIVで、EXCELの機能と同じように、-9を得られますか?
どちらの結果(-9もしくは-8)が正常でしょうか?

Because the number is negative, Excel tries to "round down" and ends up with -9. This is incorrect, IMO, as the correct integer value should be -8. RPGIV is correct. No big surprise though.

Alistair Rooney
Project Leader (Electronic Business)
Tibbett & Britten SA

負数なので、EXCEL切り捨てをしようとして、結局-9になったのです。これはおかしい。私見では、正しい整数の値は、-8になるはずです。RPG iVは正しいです。ですが、そんなに驚くことでもないです。

By standard definition RPG is doing it right. The Int function (integer) takes the integer portion of a number and drops the fraction. The Integer of -8.1 is indeed -8, not -9. I think you'll find if you take the Int of 8.1 in Excel it will come out 8, not 9, unless Excel automatically rounds up.

Jim Langston

Regards,

標準的な定義からすれば、RPGは正しく動いています。INT機能(整数)は、数字の整数部分を取り出し、端数を落とします。-8.1の整数は、確かに-8であって、-9ではありません。EXCELが自動的に切り上げをしない限りは、EXCELで8.1の整数をとれば、8になって、9では無いことが分かるでしょう。

To help clarify somethings, below is the help text from Excel. It clearly gives an example with a negative number and what it does. Some people seem to be confusing int with rounding. Taking the int of something should give the integer portion on the number, no rounding should be done. And so the -8 would be the correct answer unless you are some programmer at Microsoft who wants to screw with the world! ;-)

Scott

明確になるように、EXCELからのHELPテキストを掲げます。負数を使って、そして行っていることの例をはっきり、しめしています。何人かの人々は、INTを丸めと間違えているように思います。何かの整数をとると言うことは、数字の上の整数部分与えることであって、丸目は起きないのです。そして、だからこそ、世間をかき回したいと願うマイクロソフトのプログラマー出ないかぎり、-8が正しい答えとなるでしょう。

------------------------------------------------------------------------
Int Function, Fix Function:

Returns the integer portion of a number.

Syntax

Int(number)
Fix(number)

Elements

The number argument can be any valid numeric expression. If number contains no valid data, Null is returned.

Remarks

Both Int and Fix remove the fractional part of number and return the resulting integer value. The difference between Int and Fix is that if number is negative, Int returns the first negative integer less than or equal to number, whereas Fix returns the first negative integer greater than or equal to number. For example, Int converts -8.4 to -9, and Fix converts -8.4 to -8. Fix(number) is equivalent to: 

Sgn(number) * Int(Abs(number)) 

For me, the normal would be -8. However, it depends on the result that you desire. If you are rounding or otherwise wanting the nearest whole number then -8 would be normal. The funny thing about Excel is that int(8.9)= 8. This is because the Excel function rounds down. Now, if wanting to rounding or otherwise wanting to drop all decimals and get just the interger value, then -9 would be normal. I hope this helps. 

Bill
Programmer
Brambles Equipment Services, Inc.

私には、-8が普通です。しかしながら、それはあなたが望む結果に左右されることです。もし、切り捨てているか、ないしは、最も近い整数をほしいというなら、-8が普通です。EXCELのおかしな点は、INT(8.9)=8ということです。これは、EXCELが、端数の切り捨てをしたためです。さて、もし、丸めをしたい、ないしは、全ての小数を切り捨て、ただ単に整数を求めているのならば、-9が普通なのでしょう。お役にたちますように

Add .9 and don't round.

.9を足して、丸めをするな。
Based on the int(-8.1)=-9, I'd do

If OldVal < 0
Eval NewVal = %Int(OldVal - 1)
Else
Eval NewVal = %Int(OldVal)
EndIf

You might want to recheck the Excel function though. The not-Excel spreadsheet I use has several different functions: @int() that behaves like %Int, a @round() which adjusts to the closest significant value, a @roundup() that goes to the greater signifigant value (-8 in this case), and a @rounddown() that goes to the lesser (-9).

Personally, I like how %int behaves, because it's very easy to use it to mimic the behavior of all the other listed functions. The most complicated is probably the one above.

Peter H. Coffin
Database Application Management Tools Developer

(-8.1)=-9に準拠して、私なら、こうします。

If OldVal < 0
Eval NewVal = %Int(OldVal - 1)
Else
Eval NewVal = %Int(OldVal)
EndIf


それでも、EXCEL関数を再検査したいでしょう。私の使っているEXCELではない表計算は、いくつかの異なった関数を持っています。%intのように働く@int()、直近の意味のある値への調整をする@round()、より大きな意味のある値にする@roundup()(このケースでは-8)、より大きな意味のある値にする@rounddown()(このケースでは-9)。

個人的には、%intの働き方が気に入っています。なぜなら、その他の一覧にした関数の全てを模倣するために、%intを使うのが、とても簡単だからです。最も込み入っているのは、多分、上のものでしょう。

Excel is wrong, based on what I learned in school (round down if less than five after the decimal, up if greater; if equal I seem to recall that you went to the nearest even number, which seems a bit arbitrary, but you have to do something). There was an article about this in PC Pro recently. Excel and VBA are inconsistent, if I remember correctly. 

So if you do that formula in Excel itself you get the result you see, but if you use the int() function in a VBA macro you get a result that matches RPG.

Having said that, I see that the value in your subject line doesn't match that in the body. My comments above assume that you meant -8.1. If you meant -8.9, then RPG and VBA are wrong, IMHO. I know which way I'm betting.

Cheers,

Martin.

私が学校で学んだことによれば、EXCELが間違ってます。((学んだこととは、)小数以下で5より小さければ、切り捨てるし、5より大きければ、または5と同じならば切り上げます。一番近い偶数になったとも思えますが(意味不明)、それはまあ自由に出来ることですけど、あなたは何かしなくてはならないはずです。)最近のPC Proにこれに関する記事がありました。もし、私の記憶が正しければ、EXCELとVBAは矛盾しています。だからもし、あの公式をEXCEL自身で行うとあなたが見たとおりの結果になります。しかし、もし、VBAマクロのINT()関数を使えば、RPGと一致した結果になります。

と言ったところで、あなたのSubjectの中身と本文が一致していないことに気が付きました。私の上述のコメントは-8.1を意味していたと仮定してのことです。卑見では、もし、-8.9ならば、RPGとVBAは誤っています

※私は、うっかり、Subejectでは、int(-8.9)=-9 or -8と書いて、本文には、int(-8.1)=-9と書いてしまったのです。

A quick check shows that

MS-DOS Qbasic 1.0 says int(-8.1) = -9
MS QuickBasic 4.5 says int(-8.1) = -9
VB 6.0 says int(-8.1) = -9
VBA with Excel 97 also shows int(-8.1) = -9

So these releases are at least consistent, if not correct.

If you want RPG IV to match Excel, you've got a problem!

Peter Dow
Dow Software Services, Inc.

ちょっと調べたら、こうなっています。 

MS-DOS Qbasic 1.0 では int(-8.1) = -9 になります。
MS QuickBasic 4.5 では int(-8.1) = -9 になります。
VB 6.0 では int(-8.1) = -9 になります。
VBA with Excel 97 ではまた int(-8.1) = -9 になります。

だからこれらのリリースでは、間違っているとしても、少なくとも矛盾はありません。

もし、RPGIVをEXCELと一致させるなら、問題をかかえますよ!

>Date: Wed, 1 Mar 2000 14:14:30 -0000
>From: "McCallion, Martin" <MccalliM@Midas-Kapiti.com>
> ...
>Having said that, I see that the value in your subject line doesn't
>match that in the body. My comments above assume that you meant -8.1.
>If you meant -8.9, then RPG and VBA are wrong, IMHO. I know which way
>I'm betting.
> ...
Martin, %int always just drops the decimals. %inth (h for half-adjust) rounds up or down the way you want (with negative numbers getting rounded to the nearest number). 
| %int | %inth
-------+------+------
-8.7   | -8   | -9
-8.3   | -8   | -8
 8.7   |  8   |  9
 8.3   |  8   |  8

By the way, %dec and %dech also work this way.
Barbara Morris


マーチン、%intはいつも、小数点を切り捨てます。%inth(hはhalf-adjust(四捨五入)のh)は、お望み通りに切り上げたり、切り下げたりしますよ。(負数は一番近い数字に丸められます)

| %int | %inth
-------+------+------
-8.7   | -8   | -9
-8.3   | -8   | -8
 8.7   |  8   |  9
 8.3   |  8   |  8

ちなみに、%dec,%dechもまた、このように動きます。

※実はバーバラさんは、別のところでも助けていただいた事があるのですが(ご本人はお忘れでしょう)、IBMトロント研究所のコンパイラ担当のチームメンバーの方です。私は、関数floor()のことは、遠慮して聞きませんでした。いつも、積極的に参加して下さる姿に感動すらしています。いつもありがとう。

Bill,

I've seen Int do different things on different machines, different languages, etc.. I have seen it more and more common, however, for Int to simply truncate the decimal portion and keep the integer portion. Then you would use a Round function if you wanted to round it. Then again, I've seen Int round the number, and then you had to use a function called Trunc to truncate it *shrug*

I don't think there really is a standard yet, though, and probably there will never be one. So, before using Int on any machine, you should check to see what it is actually doing first. Round is pretty standard, but then again, some will round -8.6 to -9, and some will round to -8. 

Regards,

Jim Langston

Bill Brosch wrote:

> I thought that integer functions were always supposed to round to the
> nearest integer towards, in effect truncating any decimal portion. 8.9
> becomes 8 and -8.9 becomes -8.

Bill,

INTが、異なるマシンで、異なる言語で、異なる事をするのを見てきました。しかしながら、INTが、単純に、小数以下を切り捨てて、整数部分を保つというのは、もっともっと、普通のことです。もし、それを丸めたければ、ROUND関数を使えばいいでしょう。その反面、INTが数字を丸めるのが分かったので、それを切り捨てるのにTRUNC関数を呼ばねばなりませんね。(肩をすくめる)

まだ標準が本当にあるとは思わないけど、でも、多分一つになる事は決してないでしょう。だから、どんなマシンでも、INT関数を使う前には、最初に、実際にやっていること、を確かめるべきです。丸めは、少し標準的ですけど、その反面、有る物は-8.6を-9にまるめ、有る物は-8に丸めます。

Hi

By the definition of %int( ) , it will return the whole number left side of the decimal part , it should drop the fractional part. there is no question of round figure.

For me %int(-8.9) is -8. so RPG IV %int( ) function is returning the right value.

Jaydeep

%int()の定義からすると、小数点の左側の全体の数字をもどして、端数の部分は切り捨てるでしょう。数字を丸める疑いはありません。

私には、%INT(-8.9)は-8です。だからRPG iVの%INT()関数は、正しい値を返しています。

Checking MSDN it reports that command INT removes the fractional part of a number and returns the resulting integer value. BUT.. It also states that 

INT(number)
If number is negative , INT returns the first negative integer less than or equal to number. For example, INT converts -8.4 to -9. 

Well, thats where microsoft stands on the int function. Can you find a description in the ILE/RPG manual for this? What does it say?

MSDNを調べてみたら、INTコマンドは、数字の端数の部分を取り除き、結果として整数の値を返します。

でも、こうとも言っています。

INT(数字)
もし数字が負数の場合は、INTは、より小さいか同じ、最初に見つかるマイナスの数字を戻します。たとえば、INTは-8.4を-9に変換します。

さて、これが、マイクロソフトがとる、INT関数の立場です。ILE/RPGのマニュアルの中で、これに関する、記述がありますか?なんと言っていますか?

The IBM ILE/RPG manual (V) says:

"%INT converts the value of the numeric expression to integer. Any decimal digits are truncated. This built-in function may only be used in expressions. %INT can be used to truncate the decimal positions from a float
or decimal value allowing it to be used as an array index."

and

"%INTH is the same as %INT except that if the numeric expression is a decimal or float value, half adjust is applied to the value of the numeric expression when converting to integer type. No message is issued if half adjust cannot be performed."

No mention of sign. 

> Checking MSDN it reports that command INT removes the fractional part of a
> number and returns the resulting integer value.
> BUT..
> It also states that
>
> INT(number)
> If number is negative , INT returns the first negative integer less than
or
> equal to number. For example, INT converts -8.4 to -9.
>
> Well, thats where microsoft stands on the int function. Can you find a
> description in the ILE/RPG manual for this? What does it say?

IBMのILE/RPGマニュアル ではこう言っています(日本語マニュアルを引用しました

%INT(数値式)

%INT は、数値式の値を整数に変換します。 10 進数はすべて切り捨てられます。この組み込み関数は式の中だけで使用することができます。 %INT を使用すると、浮動値または 10 進数値から小数点以下の桁を切り捨て、その値を配列指標としすることができます。

そして、

%INTH(数値式)

%INTH は %INT と同じですが、数値式が 10 進数値または浮動値の場合には、整数タイプへの変換時に数値式の値に四捨五入が適用される、という点が異なります。四捨五入が実行できない場合、メッセージは出されません。

符号に関してはふれていません。

And Perl returns -8, and according to my instructor this is correct since the function of %int(-8.9) is to return the integer value of the number w/o first rounding the number.

Who's right? 

そして、Perlでは、-8を返してきます。そして、インストラクターによれば、これが正しいそうです。なぜなら、%int(-8.9)の機能は、第1に数字をまるめることなしに、数字の整数値をもどすこと、だからです。

だれが正しいの?


-----Original Message-----
From: Peter Dow 
Sent: Wednesday, March 01, 2000 4:53 PM
Subject: Re: int(-8.9)=-9 or -8

A quick check shows that

MS-DOS Qbasic 1.0 says int(-8.1) = -9
MS QuickBasic 4.5 says int(-8.1) = -9
VB 6.0 says int(-8.1) = -9
VBA with Excel 97 also shows int(-8.1) = -9

So these releases are at least consistent, if not correct.

If you want RPG IV to match Excel, you've got a problem!

Peter Dow
Dow Software Services, Inc.

Look this is all very simple. An Integer value of anything is the value of whole numbers without the decimals. Microsoft, in the usual way, have tried to re-invent this basic concept. Personally I think it's a bug, but MickySoft say "no, we don't just truncate the decimals, we round down!". Which explains why negative numbers get screwed up. The usual MS mantra "It's not a bug it's a feature".

They are wrong. All the products quoted by Peter are Microsoft products.

IBM, for once, have followed the mathematically correct convention. They are
correct according to 99.999% of the world's mathematicians. (The others
obviously work for MickySoft).

Why do you think Microsoft runs it's business on 400's?

Alistair (MCSE)

ほら、これは全部単純です。どんな数字でも整数値は、小数を取り除いた数字全体の値です。マイクロソフトでは、普段のとおり、この基本的な概念を再発明しようとしています。個人的には、これはバグですが、MickySoft(Micro$oft)は、「私たちは、小数以下を切り捨てるだけではないです。私たちは、丸めています!」と言っています。そして、負数が混乱していることも説明しています。いつものMSのマントラ、「バグでなくて、それはフィーチャーです。」です。

彼らは間違っています。Peterが引用した全ての製品は、マイクロソフトの製品です。

今度に限っては、IBMは、数学的に正しい協定に従いました。彼らは、世界の数学の99.999%に準じて、正しいです。(その他は、明らかにMickySoft(Micro$oft)のために尽くしています。)

どうして、400の上で、マイクロソフトがビジネスをすると思うのですか?

-----Original Message-----
Behalf Of Laskosky, Matt
Sent: 02 March 2000 01:37
Subject: RE: int(-8.9)=-9 or -8

And Perl returns -8, and according to my instructor this is correct since
the function of %int(-8.9) is to return the integer value of the number w/o
first rounding the number.

Who's right?

Pl. don't rely on these seasonal products.

プラス、これらの季節的な商品に頼ってはいけません。

※季節で変わるのですね、これらは。...そうですね。

However if you use the trunc function in excel, it should just drop the fraction/decimal position...

tim

でも、EXCELでこのtrunc関数を使えば、端数・小数点部分を切り捨てるはずです。

> -----Original Message-----
> From: Peter Dow 
> Sent: Wednesday, March 01, 2000 1:53 PM
> Subject: Re: int(-8.9)=-9 or -8

> A quick check shows that

> MS-DOS Qbasic 1.0 says int(-8.1) = -9
> MS QuickBasic 4.5 says int(-8.1) = -9
> VB 6.0 says int(-8.1) = -9
> VBA with Excel 97 also shows int(-8.1) = -9

> So these releases are at least consistent, if not correct.

> If you want RPG IV to match Excel, you've got a problem!

> Peter Dow
> Dow Software Services, Inc.

Seems open to interpretation, but my definition of any %int function 
would be "the integer portion of". By this definition, -8 is correct. 
"The next smallest integer" seems a tad arbitrary to me.

-- 
Paul Cunnane

解釈を公開するようだけど、%INT関数の私の定義では、「その整数部分」になります。この定義によれば、-8が正しいです。「その次に最小の整数」というのは、少し独断に思えます。

______________________________ Reply Separator ______________________________
___
Subject: RE: int(-8.9)=-9 or -8
Author: "Laskosky; Matt" 
Date: 2000-03-02 6:36 am
And Perl returns -8, and according to my instructor this is correct since 
the function of %int(-8.9) is to return the integer value of the number w/o 
first rounding the number.

Who's right? 
これは、自分からのお礼を兼ねたメッセージです。下手な英語を笑って下さい。

Hi, all

I'm glad to receive a lot of advices. Thank you so much indeed.

And I want to explain why I posted such message. This will be a caution for the programmers (maybe <g>).

I introduced the Zeller's formula as a column of computer tips in my home page for Japanese programmers. (Of course I know there is a function for calculation of the day of week in RPG iv.)

Zeller's formula is known as below. There are two types. 

(I) (yy2+[yy2/4]+[yy1/4]+[26(mm+1)/10]-(2*yy1)+dd)mod 7 
(II) (yyyy+[yyyy/4]-[yyyy/100]+[yyyy/400]+[(2.6*mm)+1.6]+dd) mod 7
(In Jan. and Feb. mm must be 13,14 in the last year).

On March 1st, 2000 one visitor asked 'The formula (I) returned a wrong data'. So I uploaded the EXCEL example and I showed it worked correctly for him.

But, the other visitor asked me again 'Why does (-24) mod 7 come to 4? Please explain it.'

I was confused.

February 29th,2000 = 99+[99/4]+[19/4]+[26(14+1)/10]-(2*19)+29) mod 7
= (99+24+4+39-38+29)mod 7 = (157) mod 7 = 3 (=Tuesday)

March 1st,2000 = 00+[00/4]+[20/4]+[26(3+1)/10]-(2*20)+1) mod 7 
= (00+00+5+10-40+1)mod 7 = (-24) mod 7 = 4 (=Wednesday)

Certainly I could get the day of week, so it seemed to work well. BUT, what was mod(-24,7)=4 ??? I felt it strange.  The RPG code (DIV and MVR) returned -3. (i.e. -24-(-3 *7)=-24+21=-3).

When I saw the help text of the MOD function in Excel, I was surprised. In the help of mod it says,

MOD(n,d) = n - d*INT(n/d)
<Examples>
MOD(3,2) = 1
MOD(-3,2) = 1 <===

Hey! Why mod(-3,2)=1??? I think it must be -1.

Oh, IIIII Seeeee. INT is in there! Can you see the 'INT' in MOD(n,d) above?  And in the help of INT,it says INT(8.9)=8, INT(-8.9)= -9.So, now I can see why MOD(-3,2)=1, and MOD(-24,7)=4.

MOD(-3,2)=-3-2*INT(-3/2)=-3-2*INT(-1.5)=-3-2*(-2)=-3+4=1.
MOD(-24,7)=-24-7*INT(-24/7)=-24-7*INT(-3.4)=-24-7*(-4)=-24+28=4.

It still seems a strange thing for me. Can you accept this result?

That's why I posted my message.

After I posted the message, I checked the book of an algorithm of C language, and I knew there were some functions, named FLOOR,or CEIL.

floor(2.718)=2, floor(-2.718)=-3
ceil(2.718)=3, ceil(-2.718)=-2 

※オリジナルでは、ceilingと書いていましたが、ceilが正しかったので、ここでは修正して、書きました。

If these functions are real one, INT behaves like FLOOR. 
BTW, what is floor or ceil for ? I don't know C Language well. I bought the book of an algorithm of C as my pillow <g>.

Anyway, I use often the EXCEL as a debugging tool (for confirmation of the result of programs that I made). 
And you should pay attention to 'negative numbers in MOD or INT' in EXCEL.

*------------*-------------*--------------*

I'm sorry for my poor English, do you make out what I mean? I'm uncertain of it. All I can do here is just a spell-checking.

※こんな、土下手なえいごなのに、よく分かるからsorryと書かなくてもいいよと、何人かの人が、DMくれました。いい人たちなんだなぁ。Thank you so much.

HTH

Best regards.

I really appreciate your kindness ! 

2000-3-3 


[ Previous ] [ HOME ] [ Upper ] [ Next ]

You are at K's tips-n-kicks of AS/400

 

SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送