{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/3896","labels_url":"https://api.github.com/repos/dotnet/roslyn/issues/3896/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/roslyn/issues/3896/comments","events_url":"https://api.github.com/repos/dotnet/roslyn/issues/3896/events","html_url":"https://github.com/dotnet/roslyn/issues/3896","id":94355588,"number":3896,"title":"Proposal: \"strictfp\"/\"fastfp\" modifier","user":{"login":"mburbea","id":213233,"avatar_url":"https://avatars.githubusercontent.com/u/213233?v=3","gravatar_id":"","url":"https://api.github.com/users/mburbea","html_url":"https://github.com/mburbea","followers_url":"https://api.github.com/users/mburbea/followers","following_url":"https://api.github.com/users/mburbea/following{/other_user}","gists_url":"https://api.github.com/users/mburbea/gists{/gist_id}","starred_url":"https://api.github.com/users/mburbea/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mburbea/subscriptions","organizations_url":"https://api.github.com/users/mburbea/orgs","repos_url":"https://api.github.com/users/mburbea/repos","events_url":"https://api.github.com/users/mburbea/events{/privacy}","received_events_url":"https://api.github.com/users/mburbea/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-07-10T17:40:07Z","updated_at":"2015-07-13T14:21:49Z","closed_at":null,"body":"I'm not sure which repo this issue belongs in, as it is both a language and compiler feature.\r\nCurrently, the CLR makes no guarantee about the result of floating point operations. In cases where applications communicate to each other and need to simulate floating point results locally, the lack of strict mode causes their to be differences in the result of calculation.  \r\n\r\nI propose a modifier that can be placed on either a class, method or as a standalone block.\r\n```csharp\r\npublic strictfp static class MathExtensions\r\n{\r\n// all calculations in this class will be performed in strict mode.\r\n}\r\n...\r\npublic strictfp float DoIt(float x, float y)\r\n{\r\n// the entire method will be strict\r\n}\r\n...\r\npublic double ComputeVal()\r\n{\r\n... not strict\r\n     strictfp\r\n    {\r\n      // strict math in this block.\r\n     }\r\n}\r\n...\r\n```\r\nThis should Similar to the method modifier available in Java. When strict mode is enabled, anything computed on a single or double would be truncated to either 32 or 64 bits of precision, and when a fused operation like a multiple and add operation would be called each should be called separately to avoid producing a non-reproducible result.\r\n\r\nAll of these operations should be done using SSE operators, which I believe should always be reproducible.\r\n\r\nAs a nice to have, it'd be good if the trig functions were to switch to a strict implementation as well if the current implementation is not.\r\n\r\nAs part of this proposal, I also suggest adding the opposite modifier \"fastfp\", when in a block that would be in strict mode, \"fastfp\" would result in switching back to the normal \"fast\" behavior. "},"comment":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/comments/120944928","html_url":"https://github.com/dotnet/roslyn/issues/3896#issuecomment-120944928","issue_url":"https://api.github.com/repos/dotnet/roslyn/issues/3896","id":120944928,"user":{"login":"mburbea","id":213233,"avatar_url":"https://avatars.githubusercontent.com/u/213233?v=3","gravatar_id":"","url":"https://api.github.com/users/mburbea","html_url":"https://github.com/mburbea","followers_url":"https://api.github.com/users/mburbea/followers","following_url":"https://api.github.com/users/mburbea/following{/other_user}","gists_url":"https://api.github.com/users/mburbea/gists{/gist_id}","starred_url":"https://api.github.com/users/mburbea/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mburbea/subscriptions","organizations_url":"https://api.github.com/users/mburbea/orgs","repos_url":"https://api.github.com/users/mburbea/repos","events_url":"https://api.github.com/users/mburbea/events{/privacy}","received_events_url":"https://api.github.com/users/mburbea/received_events","type":"User","site_admin":false},"created_at":"2015-07-13T14:21:49Z","updated_at":"2015-07-13T14:21:49Z","body":"I believe so. This should be viewed as the equivalent of compiling a C++ program with a fp:strict flag. \r\n\r\nIt might be slower, but predictable results are important in many applications. Especially in games where different hardware producing different results can lead to synchronization issues. \r\n\r\nCurrently, there is a work around in C# but its awful, and requires you to basically write your code like so:\r\n```csharp\r\npublic float SomeComputations(float x, float y)\r\n{\r\n      var s1 = (float)(x-y);\r\n      var s2= (float)(y-x);\r\n      var s3= (float)(s1*s2);\r\n      return (float)s3*s3;\r\n} \r\n```\r\nIf you throw in a cast at every step, the JIT will truncate at each step. Another alternative is to stick your working variable into an array, which should round trap the operation through memory (which is slow).\r\n\r\nHowever, if you only stick with SSE registers you don't have to worry about the extra precision. Also before someone invokes Eric Lippert, this is an awful answer http://stackoverflow.com/a/14864871. Using integers is hardly an acceptable solution.\r\n"}}