{"action":"created","issue":{"url":"https://api.github.com/repos/MikeMcl/big.js/issues/52","labels_url":"https://api.github.com/repos/MikeMcl/big.js/issues/52/labels{/name}","comments_url":"https://api.github.com/repos/MikeMcl/big.js/issues/52/comments","events_url":"https://api.github.com/repos/MikeMcl/big.js/issues/52/events","html_url":"https://github.com/MikeMcl/big.js/issues/52","id":85274056,"number":52,"title":"valueOf should throw an exception instead of just being an alias of toString","user":{"login":"anarchocurious","id":4643450,"avatar_url":"https://avatars.githubusercontent.com/u/4643450?v=3","gravatar_id":"","url":"https://api.github.com/users/anarchocurious","html_url":"https://github.com/anarchocurious","followers_url":"https://api.github.com/users/anarchocurious/followers","following_url":"https://api.github.com/users/anarchocurious/following{/other_user}","gists_url":"https://api.github.com/users/anarchocurious/gists{/gist_id}","starred_url":"https://api.github.com/users/anarchocurious/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/anarchocurious/subscriptions","organizations_url":"https://api.github.com/users/anarchocurious/orgs","repos_url":"https://api.github.com/users/anarchocurious/repos","events_url":"https://api.github.com/users/anarchocurious/events{/privacy}","received_events_url":"https://api.github.com/users/anarchocurious/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-06-04T20:57:14Z","updated_at":"2015-06-05T17:26:53Z","closed_at":null,"body":"Since the interpreter prefers the result of `valueOf` when coercing an object to a number and `toString` when coercing a number to a string, having `valueOf` throw an exception instead of just returning a string value could help users track down accidental conversions to the native number type (this is especially true when integrating into an existing codebase).\r\n\r\nFor example:\r\n```javascript\r\nvar a = Big('1');\r\nvar b = Big('3');\r\nvar c = Big('20')\r\n\r\nif(a < b) console.log('This works correctly in some cases...');\r\nif(c > a) console.log('...and that\\'s what makes bugs like this easy to overlook.');\r\nif(c > b) console.log('The statement is not run because \"20\" is lexically less than \"3\".');\r\n```\r\n\r\nConsider a patch like this:\r\n```javascript\r\nBig.prototype.valueOf = function valueOf() {\r\n  throw new TypeError('Conversion of Big.js object to native number is not supported.');\r\n};\r\n```\r\n\r\nNow:\r\n```javascript\r\nif(a < b) console.log('An exception is thrown before this is executed.');\r\n+a; // => throws TypeError\r\nNumber(a); // => throws TypeError\r\nString(a); // => \"1\"\r\n```\r\n\r\nIf breaking backward compatibility is an issue, this could be placed behind a flag (`Big.whinyValueOf`?).\r\n\r\nI'd be happy to write a patch, add some tests, and submit a pull request if this is something others think may be helpful."},"comment":{"url":"https://api.github.com/repos/MikeMcl/big.js/issues/comments/109370074","html_url":"https://github.com/MikeMcl/big.js/issues/52#issuecomment-109370074","issue_url":"https://api.github.com/repos/MikeMcl/big.js/issues/52","id":109370074,"user":{"login":"MikeMcl","id":921201,"avatar_url":"https://avatars.githubusercontent.com/u/921201?v=3","gravatar_id":"","url":"https://api.github.com/users/MikeMcl","html_url":"https://github.com/MikeMcl","followers_url":"https://api.github.com/users/MikeMcl/followers","following_url":"https://api.github.com/users/MikeMcl/following{/other_user}","gists_url":"https://api.github.com/users/MikeMcl/gists{/gist_id}","starred_url":"https://api.github.com/users/MikeMcl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MikeMcl/subscriptions","organizations_url":"https://api.github.com/users/MikeMcl/orgs","repos_url":"https://api.github.com/users/MikeMcl/repos","events_url":"https://api.github.com/users/MikeMcl/events{/privacy}","received_events_url":"https://api.github.com/users/MikeMcl/received_events","type":"User","site_admin":false},"created_at":"2015-06-05T17:26:53Z","updated_at":"2015-06-05T17:26:53Z","body":"I think it's more in keeping with JavaScript's flexible type system and the no-fuss, easy-to-use ethos of *big.js* that a Big number allow itself to be coerced to a primitive number when used in an arithmetic or Math operation. \r\n\r\nYou \"*imagine* this mainly happening by mistake\", but I think the contrary is more likely. \r\n\r\nI appreciate your analysis, and agree that it would be more safe and perhaps more proper to throw, but I would prefer to err on the side of being too permissive than too strict.\r\n\r\nThe original reason for `valueOf` returning a string rather than a number, was that only the former represents the full 'value of' a Big number.\r\n\r\nThere is also the option of adding a `toNumber` method, but when I considered that before, #21, I didn't think it necessary. \r\n\r\nI'm not yet rejecting your suggestion, and I'll think about it further.\r\n\r\n"}}