Introvert Developer who love to learn new Knowledge, Reading Books, Writing Blog, Drawing, play Badminton and Table-Tennis --
Founder and Writer at https://www.tamemo.com
Location
Bangkok, Thailand
Education
Computer Science, ICT Mahidol University
Work
Senior Software Engineer at Centrillion Technology
as
ทำงานก่อน/
ครับ ต้องใส่()
ด้วย ตามตัวอย่างในบทความเลยครับnote: หากต้องการลองรันโค้ด Dart สามารถเข้าไปลองรันได้ด้วยตัวเองที่ dartpad.dartlang.org/ เลยครับ
ไปลองในเว็บออนไลน์ละครับ กลายเป็นว่าได้คำตอบเป็น 0 ... เพราะว่ามันแปลง dart ไปเป็น js แล้วค่อยรันโค้ดผ่านเว็บเลย (ไม่ได้ส่งไปรันฝั่งเซิฟเวอร์) ทีนี้ js ไม่มีตัวเลขเป็น int อยู่แล้ว มีแต่ double 64 bit ถ้าเอามาเก็บ int คือเก็บได้ 53 bit แต่ dart ยอมให้เก็บ int แค่ 32 bit ดังนั้นไม่ว่ายังไง ~/ กับ / แล้วค่อยแปลงเป็น int ก็เลยได้คำตอบไม่ต่างกันอยู่แล้ว
ทีนี้ถ้าเอาโค้ดด้านบนไปลองบน dart ในเครื่องที่เป็น 64 bit (ผมใช้ 2.7.0 บน ubuntu) กลายเป็นว่าใช้ as int ไม่ได้เลยครับ ถูกด่ากลับมาแบบนี้
type 'double' is not a subtype of type 'int' in type cast
ส่วนโค้ดนี้ก็ทำให้เห็นชัดว่า ~/ ไม่เท่ากับ / แล้วแปลงทีหลังด้วย .toInt ครับ
int x = 1 << 62;
int y = x % 3;
int ans1 = x ~/ 3;
int rvt1 = 3*ans1 + y;
int ans2 = (x / 3).toInt();
int rvt2 = 3*ans2 + y;
print('x: $x');
print('');
print('ans1: $ans1');
print('rvt1: $rvt1');
print('same: ${x == rvt1}');
print('');
print('ans2: $ans2');
print('rvt2: $rvt2');
print('same: ${x == rvt2}');