ไปลองในเว็บออนไลน์ละครับ กลายเป็นว่าได้คำตอบเป็น 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}');
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
ไปลองในเว็บออนไลน์ละครับ กลายเป็นว่าได้คำตอบเป็น 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}');