spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Buffer reading and writing floats


Floating point numbers ("floats") are never a fully-accurate representation of a number; this is a common feature that is seen across multiple languages, not just JavaScript / NodeJS. For example, I encountered something similar in C# when using float instead of double.

Double-precision floating point numbers are more accurate and should better meet your expectations. Try changing the above code to write to the buffer as a double instead of a float:

var b = new Buffer(8);
b.fill(0);
b.writeDoubleBE(3.14159, 0);
b.readDoubleBE(0);

This will return:

3.14159

EDIT:

Wikipedia has some pretty good articles on floats and doubles, if you're interested in learning more:

SECOND EDIT:

Here is some code that illustrates the limitation of the single-precision vs. double-precision float formats, using typed arrays. Hopefully this can act as proof of this limitation, as I'm having a hard time explaining in words:

var floats32 = new Float32Array(1),
    floats64 = new Float64Array(1),
    n = 3.14159;

floats32[0] = n;
floats64[0] = n;

console.log("float", floats32[0]);
console.log("double", floats64[0]);

This will print:

float 3.141590118408203
double 3.14159

Also, if my understanding is correct, single-precision floating point numbers can store up to 7 total digits (significant digits), not 7 digits after the decimal point. This means that they should be accurate up to 7 total significant digits, which lines up with your results (3.14159 has 6 significant digits, 3.141590118408203 => first 7 digits => 3.141590 => 3.141590 === 3.14159).


Categories : Javascript

Related to : Buffer reading and writing floats
Reading floats from a file in R
In R, the presentation of information is different from the underlying storage of information. For example, you can use the digits option to control the number of significant digits: > print(3.1415928, digits=2) [1] 3.1 > print(3.1415928, digits=4) [1] 3.142 > options(digits=4) > print(3.1415928) [1] 3.142 > options(digits=7) > print(3.1415928) [1] 3.141593 The default i

Categories : R
sprintf / snprintf not correctly writing to buffer
I believe your problem is in your first read() call. You do a single read, expecting to get all of the welcome message. But that isn't how TCP works. TCP puts the stream data into packets according to its own internal rules, and the receiving system can make it available in any amount it wants. You cannot rely on getting all of the data the server wrote in a single read. In fact the server is me

Categories : C
Reading and writing from a csv file
This will read the contents into a collection and you can get the number of records from the collection. Sub Main() Dim FileName As String Dim NumberOfRecords As Integer FileName = "N:2_7_towns(2).csv" 'read the lines into an array Dim lines As String() = System.IO.File.ReadAllLines(FileName) 'read the array into a collection of town types 'this could also be do

Categories : Vb.Net
Writing and reading a zoo object - errors
The file has a header line so try: z <- read.zoo("foo", header = TRUE, check.names = FALSE) The check.names part gives nicer looking column names but you could leave it out if that were not important.

Categories : R
Segmentation fault in reading a file and writing it to a char vector
Your handling of the std::vector<char> needs to be updated. You have not allocated any memory in the std::vector to hold any items. You are using buffer as though it is a pointer to an array of chars. refFile->read((((char*)(buffer))+pos),WIDTH); Type casting buffer to char*, as you do above, is cause for undefined behavior. You can solve poth problems by reading one character at

Categories : C++
Recently Add
Busy Indicator while page is loading
parent.document.getElementById and images path
Can not get bootstrap datetime picker to display properly
reading select box jquery issues
DC.JS Pie Chart + Select Deslect from external JavaScript function
Why would I ever need to cancelAnimationFrame()
Angular.ui alert doesn't close
HTML/JS How to position a rectangle relative to 'any' side of the parent canvas element
parse variable to xml
C# Dictionary equivalent in JavaScript
ExtJS Dashboard Panel Fit Panel Vertically
jQuery cross domain ajax call - Interpreted as script but transferred with MIME type text/xml
Reactjs map returns unexpected token }
Working with multiple pages in apache cordova and vs2013
Resize iframe to loaded content
Why 13596*0.1 is different than 13596/10?
AngularJS Validate all forms on application load
javascript open popup without menu/url/titlebar/scrollbar/anything
Can I use Razor syntax inside Javascript included in a ScriptBundle?
How to show a box for comment reply with id
parse name from user input when using jquery serialize
How to parse and load javascript object?
Marionette not navigating to the same url
Adding Google+ Sign in button to website
GRUNT - Install Grunt Packages?
how should I create the path of templateUrl property in Angular ui-router?
Update mysql database with jquery array using ajax
Run the same function on each item in a list / array
Get all ticks values (NVD3)
Button Click Event not Passing input String to MVC Controller Method
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.