double,cross-platform,precision,floating-accuracy,deterministic , Is Double Math Consistent across Multiple Platforms?

Is Double Math Consistent across Multiple Platforms?


Tag: double,cross-platform,precision,floating-accuracy,deterministic

For my deterministic physics engine, I need to confirm that calculations with doubles in C# are consistent enough across multiple platforms. Does anyone know how much the following functions differ in results? On my computer as a Windows 32 bit application, these are the results (Note: Pseudo-code):

double x = 123.123;
x * 2 = 246.246
Math.Pow (x, 2) = 15159.273129
Math.Sqrt (x) = 11.0960803890383
Math.Sin (x) = -0.56537391969734

I still don't know if the same results will turn up on other platforms or other languages (I'm using C#). If there's any more information needed, I'll supply it as promptly as possible. Please, if there's anyone with the knowledge or time to help test the calculations on other platforms, help me get past this uncertainty.


The C# standard, ECMA-334, says in section 11.1.6 Floating point types "Floating-point operations can be performed with higher precision than the result type of the operation.".

Given that sort of rule, you cannot be sure of getting exactly the same answer everywhere. "can" means some implementations may use higher precision than others, for the same calculation.

Even if testing shows that all current implementations get the same answers for all your calculations across a range of inputs, the next compiler release on any platform might change that. If you really need identical results, you need to pick a library or similar that promises identical results.

Calculations vary in how sensitive they are to small changes in inputs. At the extreme are simulations of some physical systems such as weather, where tiny changes in inputs can expand to big changes in results.

One way to get a feeling for the behavior of your simulation is to deliberately perturb some values by e.g. one part in 1e15. Does that change the results enough to matter? However, there will always be a risk that some inputs will lead to less stable conditions than others.


Function to convert double to string with given options

I want to create a function, which has parameters with value, decimal mark and number of digits after decimal mark. This function will convert double value to string with given parameters. Here is the function I created. public static string ConvertDoubleToString(double value, string decimalMark = ".", int decimalDigits = 2)...

scanf issue when reading double

I'm using MinGW on windows 7 to compile C files. My problem is a strange behaviour with scanf() to read doubles from user input. My code: int main() { double radius = 0; double pi = 3.14159; scanf("%lf \n", &radius); // after the input, it continues waiting... radius = ((radius...

Create double by appending binary representations of two ints

I'm trying to create a double by appending the binary representations of two ints. This is what I have now: int i = arc4random(); *(&i+1) = arc4random(); double d = *(double*)&i; I hoped the double pointer would also use the value in the &i+1 address, and this is indeed the...

How to invoke an Integration Service in Kony Studio that is created in the MobileFabric Console?

I am trying to invoke an integration service in a JavaScript module in Kony Studio. Previously I created the service "TestService" with the operation "getPostsByUserID" in the MobileFabric console. The service calls an API that returns an JSON object. I want to use the returned data in my app. If...

How to parse “1,234.56” in Java as Double?

I am having trouble parsing "1,234.56" in Java, a similar question recommend using French locale in number formatting, but it is parsing the result incorrectly. Here is what I have: NumberFormat format = NumberFormat.getInstance(Locale.FRANCE); Number number = format.parse("1,234.56"); System.out.println(number.doubleValue()); // Should get 1234.56, got 1.234 instead Number number = format.parse("1,234,567.89");...

how can we have double digit range drop downs in rails forms? that gerates drop down with numbers like 00,01, 02 etc, instead 0,1 , 2 etc?

How Can we have a double digit drop downs for minutes and hours selection, in rails forms? i checked the range drop downs in rails, but it is generating single digit numbers in drop down like 0, 1, 2 etc,but i wanted to generate these numbers like 00, 01,02 etc.,

Validate double number in current culture

There is a way to validate a double number in the current culture? I have tried this: var number ="10,10"; double value = double.Parse(number, NumberStyles.Float, CultureInfo.CurrentCulture); If I try this with "es-ES" (where decimal separator is ",") then the parsing works perfect, I got an double 10.10, but if I...

How to tell the difference between linux and mac

I am creating a setup file for a tarball, and I need a way to determine the exact name of the system. I tried, but it returns 'posix' regardless of whether the system is mac or linux. Is there a way to get a more specific OS name?...

>= not working, R [duplicate]

This question already has an answer here: Why are these numbers not equal? 3 answers I am running the code below. The function Ax returtns the value 1. But the ifelse statement does not recognize that Ax==1. I've tried making the output of the function double-precision and all numeric...

Xamarin using cross platform 3D engine

I am a iOS / Android developer who has started to look at Xamarin for Business apps. As a hobby I make the odd 3D game and one thing thats always got me is having to write it for 2 platforms (Yes I do know about Unity, not my bag)...

Java method returns double | int

I need a method that will accept String number as a parameter and return double if it has a remainder, or int if it is decimal. I wrote such code: private double convertToNumber(String number) { double d = Double.parseDouble(number); if (d % 1.0 == 0) { return Integer.parseInt(number); } else...

Screen flickers when setting background

I'm trying to make a simple GUI program without using JComponents. Currently, I have a BufferedImage that I draw to off screen so that it doesn't flicker (or so I thought). I made a new program here to replicate the issue: package Main; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import...

Linking against third-party libraries when doing cross-platform build in Visual Studio 2015

I am trying to compile a Shared Object (.so) with Visual Studio 2015 RC. I am linking against the Opus Codec libs in my stdafx.h: #pragma comment(lib, "..\\..\\opus-1.1-beta\\win32\\VS2010\\Win32\\Debug\\celt.lib") #pragma comment(lib, "..\\..\\opus-1.1-beta\\win32\\VS2010\\Win32\\Debug\\opus.lib") #pragma comment(lib, "..\\..\\opus-1.1-beta\\win32\\VS2010\\Win32\\Debug\\silk_common.lib") #pragma comment(lib, "..\\..\\opus-1.1-beta\\win32\\VS2010\\Win32\\Debug\\silk_fixed.lib") #pragma comment(lib,...

Using float and double for calculation, giving different results [duplicate]

This question already has an answer here: Is floating point math broken? 18 answers Why not use Double or Float to represent currency? 10 answers I am working on a project where we are calculating prices of items with 8 places of decimal points. But sometime calculation results are...

PostgreSQL - get all integer digits from double precision field for use in to_char function

As stated in the documentation of Postgres 9.0 the double precision data type has a precision of 15 decimal digits and a storage of 8 bytes, then an integer number larger than a normal bigint (8 bytes) stored in a double precision field is approximated. Correct me if I'm wrong,...

Convert char array into double [closed]

I have tried this: #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *txt1; char tam[13]; char tam1[13]; char tam2[13]; txt1 = fopen("C:/Users/Hugo/Documents/C/in2.txt","r"); if(txt1 == NULL) { printf("No se puede leer el archivo"); return 0; } fgets(tam1,sizeof(tam1), txt1); double c = atof(tam1); printf("%f",c); when I debug: tam1 is...

Convert DoubleBinding value to IntegerBinding

I have the following Problem. I want to concat a String with an DoubleBinding to an String property. Slider slider = new Slider(1, 5000, 500); Label sliderValue = new Label(); sliderValue.textProperty().bind(Bindings.concat("Sleep: ", slider.valueProperty())); This is working fine. But I don't want to display the Double Binding as an Double, I...

How to create an embeddable C-API library in Go?

I am planning to write a cross-platform app that has most of its functionality shared across all platforms (Linux, OS X, Windows, iOS, Android). These are mostly helper function (calculations, internal lists, networking etc.) so I figured it would be convenient to have those functions in a library I can...

Java: I want to get an Integer output in double datatype

class simple { public static void main() { double n, m; Scanner ip = new Scanner(; n = ip.nextDouble(); m = ip.nextDouble(); m = n * m; System.out.println("Value in " + m); } } In the above code.. My output should only use decimal poinf if my input is using...

unrecognized debugging option: 1 [enabled by default]

I am writing a cross-platform gtk application for Windows and linux in C. Today I came across the strange compiler error in the title (which I have never seen before). This happens when I build my program in Windows, but not in Linux. When I build it in Windows via...

How do I retain search engine accessibility when loading external file into html

I plan to: $('#main.sidecontent').load('/sidecontent.html'); and insert: <div class="sidecontent" id="main"></div> a div on several html pages since the content of this div is the same and I try to straighten my workflow, basically avoiding editing every single html page when this content div changes. My questions: Is this method recommended for...

Convert MATLAB type: from complex double to double

I got a vector of type double let's say vec. When I perform for instance test=vec.^ 1.4623; I end up with test as complex double type, and I am wondering if there is a way to convert test to double since I'd like to get decimal values. ps: It's worth...

Type mismatch: cannot convert from boolean to double

The error on this line is Type mismatch: cannot convert from boolean to double The thing is all the values im calculating are doubles, I'm really confused. if(price == minprice < .20 * minprice){ Its been a few years since I last programmed in Java so sorry if this is...

How to Upgrade Cordova 3.5.0-0.2.4 to any higher version?

There are many questions in stackoverflow and in other blogs which says just execute the below command to upgrade- sudo npm install -g [email protected] After executing the above command in my terminal i get the following- /usr/bin/cordova -> /usr/lib/node_modules/cordova/bin/cordova [email protected] /usr/lib/node_modules/cordova ├── [email protected] ├── [email protected] ├── [email protected] ([email protected]) └── [email protected]

NCurses like library for haskell that runs on both Windows and Unix?

I have a little program that is multi-threaded. I'm tired of the output ending up cropping up within my input. I would like to use some type of library that will move me beyond the console model of the glass teletype. The library I know (of) is ncursed which I...

precision of comparing double values with EPSILON in C

Doing function that takes 2 arrays (column1 and column2) from struct CSV D and plots the graph from it. Idea is to find max, min values of each array, then break range between min−EPSILON and max+EPSILON in to 600 equal regions, where EPSILON = 10^(−6) Problem is that function does...

PHP preg_match double quote matching

I'm a newbie, and a bit of a regex dullard, so please forgive me what will appear to be a completely stupid question. In PHP I'm trying to use a regex to match when a double quote " appears in text from a control on an HTML form and allow...

Java issue converting String to double

I'm trying to convert a String into double. The source string is in the format "88.6" and the double I expect should be "88.60". I tried converting using Double.parseDouble, Double constructor, Double.valueOf, DecimalFormat and NumberFormat hardcoding Locale.US or Locale.ENGLISH but I still get "88,600000". How should I convert it? Thanks...

Are there limits of precision on the memory capacity of double?

I'm reading an introductory book on Java and ran into something I don't quite understand. In covering variable types, the author states "the word double stands for numbers between This struck me as odd, since as written, it would include all numbers on the real number line between the two...

ionic build ios fails with “[TypeError: Arguments to path.join must be strings]”

I am trying to create an ionic app on Mac. I followed the instructions on ionic's website to create a basic app and was able to run it in the browser but when I run "ionic build ios" I get following error. I cannot find a log file or any...

Get warning when convert double to NSDecimalNumber

I'm trying to convert a double to an NSDeciamlNumber. Here is my code: - (NSDecimalNumber *)myMethod { double amount = 42; ... return [NSDecimalNumber numberWithDouble:amount]; // Get warning here } But I get the following warning: Incompatible pointer types returning 'NSNumber *' from a function with result type 'NSDecimalNumber *'...

What is the systematic way when I want my code (client and server ) work on various scenarios?

I developed in on 64-bit MAC. And I wish it would work under two scenarios: 64 bit server and 64 bit client 32 bit server and 64 bit client ONLY DEALING *NIX NOW Between the communication of server A and client B, I would exchange a linked list of struct,...

Double.ToString returns empty for 0

Could anyone help me understand why the "text" variable is empty for the below snippet, double d = 0; var text = d.ToString("##,###,###,###"); If I change the value of d to any non-zero value, I get its correct string representation....

Convert a string to a double for a if clause

I'm trying to convert a string to a double, so I can use the double to compare it with another one, to make the result green if it's the same or red when not. The first method is: public void getCords(View view) { double latitude; double longitude; latitude = 1.00;...

MIPS float printing

I wrote this code in MIPS to calculate the sum of N-numbers with double float precision. But when I print the result it prints 0.00000 althought I see that in memory it's the correct result. Anyone can help me with this? #architecture ex.2 .data first: .asciiz "Give the number of...

How do I convert a double into an n-character string using exponential notation?

I have found this vise-versa many times on SO, but never like I want it. I want to convert a double into a string that takes up exactly n characters. If n is, for example, 6, then 1,000,123.456 would become 1.00E6 -1,000,123.456 would become 1.0E-6 1.2345678 would become 1.2345 1,000,000,000,000...

VB.Net Double comparison after some additions,comparison,double
I've faced a weird case with a double variable after adding some values to it. The problem occurs when adding (0.2) to a double variable more than one time -I think it only happens with (0.2)- for example: consider this code: Dim i As Double = 2 i = i...

Normalization in Java (dataset of doubles)

I'm working on a clustering program, and have a dataset of doubles that I need to normalize in order to make sure that every double (variable) has the same influence. I would like to use min-max normalization where for every variable the min and max value are determined, but I'm...

Integer division overflows

The Problem I have been thinking about integer (type int) overflows, and it occurs to me that division could overflow. Example: On my current platform, I have INT_MIN == -INT_MAX - 1 and thus INT_MIN < -INT_MAX and thus INT_MIN / -1 > -INT_MAX / -1 and thus INT_MIN /...

Why GraphicsContext uses methods with double params?

I'm moving from swing to javafx. I wondered why GraphicsContext uses methods with double params. For example fillRect(double x, double y, double w, double h) unlike swing has methods drawRect(int x, int y, int width, int height). Should I do all my calculations in double?...

Why does program provide -143155…when I only switched from int to double? [closed]

Why does this code give a completely wrong answer if I use float or double but not int? //C How to Program Exercises 2.33 #include <stdio.h> #include <conio.h> double dailyDrivingCost(double miles, double costPerGallon,double averageMilesPerGallon); int main(void){ double a,b,c,d,e; printf ("Please enter the number of miles daily\n"); scanf("%lf",&a); printf ("Please enter...

printing a double variable with %f and the terminal is displaying a “?” [duplicate]

This question already has an answer here: Arduino: printf/fprintf prints question mark instead of float 3 answers I am completely lost on what to do with this issue. I'm not very familar with c language, but I know enough to program my Atmel MCU for a controls project. I'm...

the usage of the long double

The functions purpose is to calculate the square root of a number using the Newton-Raphson method. I included a printf routine in the while loop so I can see the value of root 2 get closer and closer to the actual value. I originally used float to define epsilon but...

Can std::uniform_real_distribution(0,1) return a value greater than 0.99999999999999994?

From the C++11 header , I was wondering if a std::uniform_real_distribution<double> object can spit out a double that's greater than 0.99999999999999994? If so, multiplying this value by 2 would equal 2. Example: std::default_random_engine engine; std::uniform_real_distribution<double> dist(0,1); double num = dist(engine); if (num > 0.99999999999999994) num = 0.99999999999999994; int test1 =...

How to Build Cross Platfrom ( Windows Phone and Android) [closed]

I am a begainner into mobile app devolpment , and I wanted to make mobile apps with any programming lang and i wated to make the app cross platfrom so i dont need to write it 2 or 3 times so and i heared about xmairin but you will need...

Ruby CSV gem returning Infinity instead of double

I have a method that is going through a CSV file and uploading to Postgres. CSV.foreach(path, converters: :all) When it encounters a number such as "2.02E+17" it is uploading "2.0150519e+17", but when it encounters "20150515E000590" it is uploading "Infinity". If I set CSV.foreach(path) When it encounters "2.02E+17" it is uploading...

Can't seem to get .doubleValue() to work (I have the latest version of Java)

So I am having problem with a method I created, called averageCost(). The error I get is: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method doubleValue() is undefined for the type Double at queue.averageCost( at queue.main( And previously I tried just adding a double value to a Double...

Calculating floating points as binary

The question is : x and y are two floating point numbers in 32-bit IEEE floating-point format (8-bit exponent with bias 127) whose binary representation is as follows: x: 1 10000001 00010100000000000000000 y: 0 10000010 00100001000000000000000 Compute their product z = x y and give the result in binary IEEE...

Multiplying variables and doubles in swift

I'm a designer looking into learning Swift and I'm a beginner. I have no experience whatsoever. I'm trying to create a tip calculator using basic code in Xcode's playground. Here is what I have so far. var billBeforeTax = 100 var taxPercentage = 0.12 var tax = billBeforeTax * taxPercentage...

Add data fromEeditText to double

I have an app where the user can enter an amount into an EditText but then I want to be able to add or subtract this to/from a double then be able to display this double with a TextView within another activity. I'm not sure how to go about this...