<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: Compilation bug?</title>
		<link>http://forums.leaflabs.com/topic.php?id=1310</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:27:22 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=1310" rel="self" type="application/rss+xml" />

		<item>
			<title>gbulmer on "Compilation bug?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1310#post-7971</link>
			<pubDate>Tue, 31 Jan 2012 17:38:21 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">7971@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;robertm - I really don't know for certain why that macro is there, but Wiring, which was used by the Arduino team, has an integer returning round().&#60;br /&#62;
See &#60;a href=&#34;http://wiring.org.co/reference/round_.html&#34;&#62;Wiring round()&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;Also Java, which influenced Wiring and Processing has an integer returning round&#60;br /&#62;
See &#60;a href=&#34;http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#round%28double%29&#34;&#62;Java round()&#60;/a&#62;.&#60;/p&#62;
&#60;p&#62;I think the Maple/Wiring language is C/C++, so should (as much as practical) leave standard libraries (like math.h) in place so that C/C++ code and documentation works.&#60;/p&#62;
&#60;p&#62;(full disclosure: I am not a member of LeafLabs staff.)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>robertm on "Compilation bug?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1310#post-7970</link>
			<pubDate>Tue, 31 Jan 2012 16:56:26 +0000</pubDate>
			<dc:creator>robertm</dc:creator>
			<guid isPermaLink="false">7970@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;gbulmer - Thanks for looking at this.&#60;/p&#62;
&#60;p&#62;I commented out that line and yes it works correctly afterwords (I'm not sure why the standard definitions are being overridden).&#60;/p&#62;
&#60;p&#62;I also started messing around with the macro to see if I could get it to work.&#60;/p&#62;
&#60;p&#62;I changed it to: #define round(x) ((long)(x)&#38;gt;0?((x)+.5):((x)-0.5))&#60;br /&#62;
This almost works but I get 123.40 from my function instead of 123.30&#60;/p&#62;
&#60;p&#62;I then changed it to: #define round(x) ((long)x&#38;gt;0?((x)+0.5):((x)-0.5))&#60;br /&#62;
This works correctly, I get 123.30 from both of my dround functions.&#60;/p&#62;
&#60;p&#62;I am not sure why changing where the casting takes place and removing the parenthesis from the first x in the ternary should make it work (I think it should be the same)and I don't know how safe removing the parenthesis is, but it does work.&#60;/p&#62;
&#60;p&#62;It's definitely some weird bug. I'll probably just leave it commented out for now.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Compilation bug?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1310#post-7969</link>
			<pubDate>Tue, 31 Jan 2012 16:08:44 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">7969@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;robertm - I commented out the #define of round(x) in the file wirish_math.h, and your code gives consistent answers.&#60;/p&#62;
&#60;p&#62;So it is the #define round(x) macro in wirish_math.h which is causing the problem.&#60;/p&#62;
&#60;p&#62;Unfortunately, someone has chosen to override the standard C definition of round().&#60;/p&#62;
&#60;p&#62;So I think it is a bug.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Compilation bug?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1310#post-7968</link>
			<pubDate>Tue, 31 Jan 2012 14:52:33 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">7968@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I've dug a bit more, and found what I believe is a bug.&#60;/p&#62;
&#60;p&#62;There is a 'recoding opportunity' (in my Mac version) in the file:&#60;br /&#62;
/Applications/MapleIDE.app/Contents/Resources/Java/hardware/leaflabs/cores/maple/wirish_math.h:&#60;br /&#62;
it says&#60;br /&#62;
&#60;code&#62;#define round(x)                ((x)&#38;gt;=0?(long)((x)+0.5):(long)((x)-0.5))&#60;/code&#62;&#60;br /&#62;
which will round a double or float value to a long, thus losing the fractional part.&#60;br /&#62;
I assume it is the same on all platforms.&#60;/p&#62;
&#60;p&#62;I tried:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;#undef round

#include &#38;lt;math.h&#38;gt;

#undef round&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;but still couldn't get the value correct.&#60;br /&#62;
Edit: so it might be a combination of a bug and that macro :-(&#60;/p&#62;
&#60;p&#62;I'd recommend you go find that file, and comment out that 'round' macro.&#60;/p&#62;
&#60;p&#62;(full disclosure: I am not a member of LeafLabs staff.)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Compilation bug?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1310#post-7966</link>
			<pubDate>Tue, 31 Jan 2012 13:58:56 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">7966@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;robertm - your round functions do not work on my v0.0.12 installation either, so it is not local to your machine.&#60;/p&#62;
&#60;p&#62;I checked on my Mac, and gcc does generate a program which gives correct, bit identical values.&#60;/p&#62;
&#60;p&#62;It looks like a compiler bug (or possibly library bug) to me.&#60;/p&#62;
&#60;p&#62;I modified your code so that I could see the bit patterns, and added a cast to double changing the line:&#60;br /&#62;
&#60;code&#62;double c = round((num - b) * a) / a;&#60;/code&#62;&#60;br /&#62;
to:&#60;br /&#62;
&#60;code&#62;double c = round((num - b) * a) / (double) a;&#60;/code&#62;&#60;br /&#62;
and the output matches, and looks correct for this example.&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;#include &#38;lt;math.h&#38;gt;

union dbits {
	double d;
	uint32 i[2];
};

void printbits(double d) {
  union dbits a;
  a.d = d;
  SerialUSB.print(d);             SerialUSB.print(&#38;quot;  &#38;quot;);
  SerialUSB.print(a.i[0], HEX);   SerialUSB.println(a.i[1], HEX); 

} 

double dround_a(double num, unsigned int ndigits) {
  int a = pow(10, ndigits);
  double b = floor(num);
//  double c = round((num - b) * a) / a;
  double c = round((num - b) * a) / (double)a;
  SerialUSB.print(&#38;quot;dround_a: c=&#38;quot;);    printbits(c);
  double x = b + c;
  return x;
}

double dround_b(double num, unsigned int ndigits) {
  int a = pow(10, ndigits);
  double b = floor(num);
  double temp = round((num - b) * a);
  SerialUSB.print(&#38;quot;dround_b: temp=&#38;quot;);    printbits(temp);
  double c = temp / a;
  SerialUSB.print(&#38;quot;dround_b: c=&#38;quot;);    printbits(c);
  double x = b + c;
  return x;
}

void setup(void) {
  SerialUSB.begin();
}

void loop(void) {
  double value = 123.3456;

  SerialUSB.println(dround_a(value, 1));
  SerialUSB.println(dround_b(value, 1));

  delay(1000);
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;(full disclosure: I am not a member of LeafLabs staff.)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>robertm on "Compilation bug?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1310#post-7964</link>
			<pubDate>Tue, 31 Jan 2012 12:55:42 +0000</pubDate>
			<dc:creator>robertm</dc:creator>
			<guid isPermaLink="false">7964@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I've got a function that I am using to round a double to a specific decimal place (example: rounding 123.3456 to 123.3) but isn't working correctly.&#60;/p&#62;
&#60;p&#62;Below is some test code I wrote. Both functions dround_a() and dround_b() should be exactly the same except dround_b() has the call to round() separated on its own line.&#60;/p&#62;
&#60;p&#62;When the code is run, dround_a() returns 123.00, while dround_b() returns 123.30 - which is correct.&#60;/p&#62;
&#60;p&#62;Is this a compiler bug causing dround_a not to work correctly, or is it something else?&#60;/p&#62;
&#60;p&#62;I'm using the Maple IDE v0.0.12 with a rev.5 Maple board.&#60;/p&#62;
&#60;p&#62;Here is the test code:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;#include &#38;lt;math.h&#38;gt;

/**
 * Rounds a number to a specified number of decimal places
 *
 * @param num  The number to round
 * @param ndigits  The number of decimal places to round to
 *
 * @return the rounded value
 */
 double dround_a(double num, unsigned int ndigits) {
  int a = pow(10, ndigits);
  double b = floor(num);
  double c = round((num - b) * a) / a;
  double x = b + c;
  return x;
}

double dround_b(double num, unsigned int ndigits) {
  int a = pow(10, ndigits);
  double b = floor(num);
  double temp = round((num - b) * a);
  double c = temp / a;
  double x = b + c;
  return x;
}

void setup(void) {
}

void loop(void) {
  double value = 123.3456;

  if (SerialUSB.available()) {
    uint8 input = SerialUSB.read();

    switch(input) {
      case &#38;#39;a&#38;#39;:
        SerialUSB.println(dround_a(value, 1));
        break;
      case &#38;#39;b&#38;#39;:
        SerialUSB.println(dround_b(value, 1));
        break;
      default:
        break;
    }
    SerialUSB.print(&#38;quot;&#38;gt; &#38;quot;);
  }
}&#60;/code&#62;&#60;/pre&#62;</description>
		</item>

	</channel>
</rss>
