บั่นทอนสมอง ตอน จาก Cmp ยัน Negative Zero

Nimit Suwannagate (EThaiZone)
E-Commerce Solution
2 min readSep 29, 2016

บันทึกบทสนทนาจากโปรแกรม Line

2016.09.29 Thursday
16:51 kong return (astts > bstts) ? (astts > bstts) ? 1 : 0 : -1;
16:51 kong มันหมายความว่าไง
16:51 kong if แบบนี้
16:51 Execter ซ้อนไง
16:52 Execter อันแรกถ้า false = -1
16:57 EThaiZone return (astts != bstts) ? (astts > bstts) ? 1 : -1 : 0;
17:01 EThaiZone return (astts == bstts) ? 0 : (astts > bstts) ? 1 : -1;
17:03 Execter ไปไกลแล้ว 555
17:05 EThaiZone return astts < bstts ? -1 : Number(astts > bstts);
17:09 kong 555
17:10 Full Stack Developer Stickers
17:15 EThaiZone return tmp = Number(astts > bstts) || tmp-1;
17:15 EThaiZone ไม่รุ้ทำไมจะเขียนสั้นได้มากกว่านี้
17:16 EThaiZone ไม่รู้ว่าทำยังไงจะพิมพ์ได้สั้นกว่านี้อีก
17:16 Full Stack Developer อยากพิมพ์สั้นๆก็ไม่บอก
17:16 Thunderbird r
17:16 iAmWeng Photos
17:16 Full Stack Developer return a();
17:16 Full Stack Developer อยากได้ไร โยน parameter ไปสิ
17:18 Execter สักจริงชื่อ function แต่ใน function ว่ากันอีกที
17:18 EThaiZone โค้ดสำหรับ lab

var a = 2;
function cmp(astts, bstts) {
return tmp = Number(astts > bstts) || tmp-1;
}
for(let b = 1; b <= 3; b++) {
console.log(‘astts = ‘ + a, ‘bstts = ‘ + b, ‘result = ‘ + cmp(a, b));
}

17:18 Full Stack Developer ์lab เคมี หรือ lab ชีวะ
17:18 EThaiZone ใครอยากลองๆ หาทางทำให้ function cmp มันทำงานได้สั้นกว่านี้ ลองดูกันนะครับ 555+
17:23 EThaiZone เหยดด อันนี้มีบั๊กกก
17:26 EThaiZone return astts < bstts ? -1 : Number(astts > bstts);
อันนี้เวิร์คสุดล่ะ

17:30 BiG Photos
17:35 EThaiZone หาเจอล่ะ สั้นสุดๆ และทำงานถูก
return Math.sign(astts - bstts);

17:36 EThaiZone https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign
17:39 EThaiZone http://stackoverflow.com/questions/7223359/are-0-and-0-the-same

Object.is(-0, +0); // false
17:42 EThaiZone -null === -0
17:42 EThaiZone http://cwestblog.com/2014/02/25/javascript-testing-for-negative-zero/
บล็อกของคนว่างจัด 555+

17:47 EThaiZone จำลองการเกิด -0

var x = 1e-200;
var y = 1e-200 * x;
console.log(y);
y = -1e-200*x;
console.log(y);

17:51 EThaiZone ต่อจากข้างบน การยืนยัน negative zero กับ zero มีค่าเท่ากัน
console.log((Math.log(-0) === -Infinity) === (Math.log(0) === -Infinity));

17:52 EThaiZone อ้างอิง http://www.johndcook.com/blog/2010/06/15/why-computers-have-signed-zero/
17:52 EThaiZone ปล. เรื่องแบบนี้ C++ ก็ซวยไปด้วยนะ
17:54 EThaiZone http://sharkdownloads.com/other/rosetta-stone-totale-5-crack-all-language-packs-full/
18:01 Thunderbird Stickers

ข้อมูลเสริม

https://en.wikipedia.org/wiki/Signed_zero

--

--