Skip to content

Float is rounded to nearest int value #515

Answered by zeux
masrul asked this question in Q&A
Discussion options

You must be logged in to vote

The value is not rounded to nearest int value - the tool you're using to print/view the value is truncating it.

~/pugixml $ cat test.cpp
#include "pugixml.hpp"
#include <stdio.h>

int main()
{
	pugi::xml_document doc;
	doc.load_string("<Bond class1='OW' class2='HW' length='0.09572' forceconst='502080.7'/>");
	float bond_fc = doc.child("Bond").attribute("forceconst").as_float();
	printf("%f\n", bond_fc);
	double bond_fc_d = doc.child("Bond").attribute("forceconst").as_double();
	printf("%f\n", bond_fc_d);
}
~/pugixml $ g++ test.cpp src/pugixml.cpp -I src && ./a.out
502080.687500
502080.700000

Single precision floating point numbers can't represent decimals exactly in general, but also hav…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@masrul
Comment options

Answer selected by masrul
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants
Converted from issue

This discussion was converted from issue #514 on September 10, 2022 15:03.