double-precision

C# High double precision

百般思念 提交于 2019-12-01 17:43:48
I'm writing a function that calculates the value of PI, and returns it as a double. So far so good. But once the function gets to 14 digits after the decimal place, it can't hold any more. I'm assuming this is because of the double's limited precision. What should I do to continue getting more numbers after the decimal place? I wouldn't do it in floating point at all. Recall that your algorithm is: (1 + 1 / (2 * 1 + 1)) * (1 + 2 / (2 * 2 + 1)) * (1 + 3 / (2 * 3 + 1)) * (1 + 4 / (2 * 4 + 1)) * (1 + 5 / (2 * 5 + 1)) * (1 + 6 / (2 * 6 + 1)) * (1 + 7 / (2 * 7 + 1)) * ... Every stage along the way

Accuracy of double precision multiplication in java?

耗尽温柔 提交于 2019-12-01 17:28:00
What is the guaranteed accuracy of multiplication operator for double values in java? For example, 2.2 * 100 is 220.00000000000003, but 220 is a double number. 220.00000000000003 is the next double after 220. The multiplication is working fine, but 2.2 cannot be represented exactly as a double. The closest doubles are: 2.199999999999999733 (0x4001999999999999) 2.200000000000000177 (0x400199999999999a) Some software will print the latter value as 2.2 , but that doesn't mean it's exact. It just means it's treated as "close enough". If you are working with financials data dont use float or double

Accuracy of double precision multiplication in java?

。_饼干妹妹 提交于 2019-12-01 15:25:28
问题 What is the guaranteed accuracy of multiplication operator for double values in java? For example, 2.2 * 100 is 220.00000000000003, but 220 is a double number. 220.00000000000003 is the next double after 220. 回答1: The multiplication is working fine, but 2.2 cannot be represented exactly as a double. The closest doubles are: 2.199999999999999733 (0x4001999999999999) 2.200000000000000177 (0x400199999999999a) Some software will print the latter value as 2.2 , but that doesn't mean it's exact. It

Floating point arithmetic is too reliable

喜你入骨 提交于 2019-12-01 11:43:15
I understand that floating point arithmetic as performed in modern computer systems is not always consistent with real arithmetic. I am trying to contrive a small C# program to demonstrate this. eg: static void Main(string[] args) { double x = 0, y = 0; x += 20013.8; x += 20012.7; y += 10016.4; y += 30010.1; Console.WriteLine("Result: "+ x + " " + y + " " + (x==y)); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } However, in this case, x and y are equal in the end. Is it possible for me to demonstrate the inconsistency of floating point arithmetic using a program of

Switching between float and double precision at compile time

两盒软妹~` 提交于 2019-12-01 08:19:50
Where should I look at if I want to switch between float and double precision at compile time. Its like, if user wants everything in float instead of double precision how I can maintain this flexibility? In other words, how should I define a variable that could be either float or double precision conditionally? If it is OK to make the switch at compile time, a simple typedef would do: #ifdef USE_DOUBLES typedef double user_data_t; #else typedef float user_data_t; #endif Use user_data_t in your code, and set USE_DOUBLES if you want doubles at compile time: g++ -DUSE_DOUBLES=1 myprogram.cpp

GLSL Double Precision Angle, Trig and Exponential Functions Workaround

好久不见. 提交于 2019-12-01 06:29:43
In GLSL there's rudimentary support for double precision variables and operations which can be found here . However they also mention "Double-precision versions of angle, trigonometry, and exponential functions are not supported.". Is there a simple workaround for this, or do I have to write my own functions from scratch? j-p this link seem's to be the best answer So yes, you'll need to make your own implementation for those functions. glibc source may be your friend. 来源: https://stackoverflow.com/questions/22522169/glsl-double-precision-angle-trig-and-exponential-functions-workaround

Switching between float and double precision at compile time

六月ゝ 毕业季﹏ 提交于 2019-12-01 05:47:24
问题 Where should I look at if I want to switch between float and double precision at compile time. Its like, if user wants everything in float instead of double precision how I can maintain this flexibility? In other words, how should I define a variable that could be either float or double precision conditionally? 回答1: If it is OK to make the switch at compile time, a simple typedef would do: #ifdef USE_DOUBLES typedef double user_data_t; #else typedef float user_data_t; #endif Use user_data_t

C++ writing and reading doubles from a binary file

旧时模样 提交于 2019-12-01 05:47:15
问题 I want to perform disk I/O operations for a program that takes too much RAM. I use matrices of doubles and think writing them to disk as bytes is the fastest way (I need to preserve the double precision). How to do it with portability? I found this code (here) but the author says it's not portable... #include <iostream> #include <fstream> int main() { using namespace std; ofstream ofs( "atest.txt", ios::binary ); if ( ofs ) { double pi = 3.14; ofs.write( reinterpret_cast<char*>( &pi ), sizeof

GLSL Double Precision Angle, Trig and Exponential Functions Workaround

≡放荡痞女 提交于 2019-12-01 04:21:20
问题 In GLSL there's rudimentary support for double precision variables and operations which can be found here. However they also mention "Double-precision versions of angle, trigonometry, and exponential functions are not supported.". Is there a simple workaround for this, or do I have to write my own functions from scratch? 回答1: this link seem's to be the best answer So yes, you'll need to make your own implementation for those functions. glibc source may be your friend. 来源: https:/

host to network double?

妖精的绣舞 提交于 2019-12-01 03:27:50
问题 I'd like to send some double precision floating point numbers over the network. (standard C, standard sockets) There is no htond or ntohd to convert the data to and from network byte order. What should I do? I have a couple solutions in my head but I'd like to know what the common practice is. (I'd also like to know what is the common practice for sending 64bit ints, like gint64 values used by gstreamer) edit: This is one solution I thought of. I presume it works for any size integers, but is