Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

Rounding strategy in Java/JavaScript/PowerShell

tl;dr

  • Java: Floor in type casting. Round half up for Math.round(). You can choose rouding strategy for BigDecimal.
  • JavaScript: Round half up for Math.round(). Round half away from zero for toFixed().
  • PowerShell (.NET CLR): Round half to even (bankers' rounding) in type casting, and [math]::Round(). You can choose rouding strategy in [math]::Round() with optional argument.

Java

java> double [] arr = {-3.5,-2.5,-1.5,-0.5,0.5,1.5,2.5,3.5};
java> for (int i=0; i<arr.length; i++) { System.out.println((int)arr[i]); }
-3
-2
-1
0
0
1
2
3
java> for (int i=0; i<arr.length; i++) { System.out.println(Math.round(arr[i])); }
-3
-2
-1
0
1
2
3
4
java> for (int i=0; i<arr.length; i++) { System.out.println(BigDecimal.valueOf(arr[i]).setScale(0,BigDecimal.ROUND_HALF_UP)); }
-4
-3
-2
-1
1
2
3
4
java> for (int i=0; i<arr.length; i++) { System.out.println(BigDecimal.valueOf(arr[i]).setScale(0,BigDecimal.ROUND_HALF_DOWN)); }
-3
-2
-1
0
0
1
2
3
java> for (int i=0; i<arr.length; i++) { System.out.println(BigDecimal.valueOf(arr[i]).setScale(0,BigDecimal.ROUND_HALF_EVEN)); }
-4
-2
-2
0
0
2
2
4

JavaScript

var arr = [-3.5,-2.5,-1.5,-0.5,0.5,1.5,2.5,3.5];
for (i in arr) { console.log(Math.round(arr[i])); }
-3
-2
-1
-0
1
2
3
4
undefined
for (i in arr) { console.log((arr[i]).toFixed()); }
-4
-3
-2
-1
1
2
3
4

PowerShell

see http://satob.hatenablog.com/entry/2017/06/01/234802 .

PowerShell does banker's rounding in type casting to int

Problem:

PowerShell rounds 0.5 to 0 (banker’s rounding) in type casting to int.

PS > @(-3.5,-2.5,-1.5,-0.5,0.5,1.5,2.5,3.5) | ForEach-Object { "{0} -> {1}" -F $_, [int]$_ }
-3.5 -> -4
-2.5 -> -2
-1.5 -> -2
-0.5 -> 0
0.5 -> 0
1.5 -> 2
2.5 -> 2
3.5 -> 4

Hey, Scripting Guy! Blog says use [math]::Round() to round numbers, but [math]::Round() does also banker’s rounding in default.

PS > @(-3.5,-2.5,-1.5,-0.5,0.5,1.5,2.5,3.5) | ForEach-Object { "{0} -> {1}" -F $_, [math]::Round($_) }       -3.5 -> -4                                                                                                              
-2.5 -> -2
-1.5 -> -2
-0.5 -> 0
0.5 -> 0
1.5 -> 2
2.5 -> 2
3.5 -> 4

Solution:

Use [math]::Round() with option "AwayFromZero" when you want to round 0.5 to 1.

PS > @(-3.5,-2.5,-1.5,-0.5,0.5,1.5,2.5,3.5) | ForEach-Object { "{0} -> {1}" -F $_, [math]::Round($_,0,"AwayFromZero") }
-3.5 -> -4
-2.5 -> -3
-1.5 -> -2
-0.5 -> -1
0.5 -> 1
1.5 -> 2
2.5 -> 3
3.5 -> 4

Supported character encodings in Get-Content and Import-Csv (in PowerShell 2.0/4.0)

Tested in Windows 7 (Japanese).

  • Import-Csv does not have -Encoding option in PowerShell 2.0.
  • There are no option for UTF-32BE in PowerShell 2.0. (note: PowerShell ISE can handle UTF-32BE Files)
  • Import-Csv does not support Unknown and String Encoding.
  • String, Default, OEM, and without -Encoding option are the same.
  • File in UTF-16 with BOM can be read even if -Encoding option is wrong
  • UTF-32BE with BOM could be misdetermined to UTF-16BE (because starting bytes are FEFF)

f:id:satob:20170601231527p:plain

Who does recommend changeing password periodically and who doesn't

Who recommends changeing password periodically

Some say you should change password periodically.

I think IPA should have another report that denies the effect of periodic password changing, but I cannot found a such information.

また、破られにくいパスワードを使っていても、長期間変更せずにいると漏えいする危険性が高まります。パスワードは、定期的に(例えば月毎)変更するようにしましょう。

Who doesn't recommend changeing password periodically

Some say you don't have to (or should not) change password periodically.

Who neither recommends nor denies changeing password periodically

Conclusion:

You can choose either one of them according to what your customer said!

2017-07-07

Add 教育情報セキュリティポリシーに関するガイドライン案 (MEXT)

2017-08-12

Add コンピュータウイルス・不正アクセスの届出状況[2010年2月分]について(IPA) and JIS Q 27001:2014

2018-01-05

2019-01-29

  • Move プライバシーマーク制度 (JIPDEC) JIS Q 15001:2006をベースにした個人情報保護マネジメントシステム実施のためのガイドライン 第2版 from who recommends to who neither recommends nor denies

2019-02-22

  • Move 医療情報システムの安全管理に関するガイドライン from who recommends to who doesn't recommend

How to convert from ADIF AAC to another format (ffmpeg and libav can't handle it)

Context:

You have some AAC ADIF files and you want to convert them into MP3 (and the like) format.

Problem:

ffmpeg and libav don’t (and will never) support ADIF AAC files because it is “bad format”.

Solution:

You can convert an ADIF AAC files into a ordinary (handled with ffmpeg) .m4a file with this adif2mp4.exe on this page: https://rtfreesoft.blogspot.jp/2009_10_01_archive.html

If you use Ubuntu Linux 16.04, you can run adif2.exe from Wine 1.6.2 (installed from Ubuntu 16.04 apt repository). For example, you can convert all *.AAC files in current directory into .m4a format:

$ for i in *.AAC; do wine ../bin/adif2mp4.exe $i `basename $i .AAC`.m4a; done

Once the files are converted to .m4a format, you can handle them with ffmpeg like this:

$ for i in *.m4a; do ffmpeg -i $i `basename $i .m4a`.mp3; done