Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 9357 - Better diagnostic on float literals #1520

Closed
wants to merge 2 commits into from
Closed

Issue 9357 - Better diagnostic on float literals #1520

wants to merge 2 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Jan 20, 2013

http://d.puremagic.com/issues/show_bug.cgi?id=9357

Opened as a more-general solution to #1514.

@@ -2475,6 +2475,9 @@ char *RealExp::toChars()
if (type->isimaginary())
strcat(buffer, "i");

if (((real_t)(dinteger_t)value) == value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this doesn't actually work, big values are not printed as integers. Maybe use strchr instead and search for a . or e?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you show a test-case where it fails?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int x = 1048575.0L;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, int x = 1048576.0L;. Large powers of two are representable in floating point types but are not printed as integers.

@ghost ghost closed this Jan 21, 2013
@@ -2465,7 +2465,7 @@ void ErrorExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)

char *RealExp::toChars()
{
char buffer[sizeof(value) * 3 + 8 + 1 + 1];
char buffer[256];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why introduce a magic number? Instead it needs a comment. sizeof(value)*3 is because each byte of mantissa is max of 256 (3 characters). The string will be "-M.MMMMe-4932". (ie, 8 chars more than mantissa). Plus one for trailing \0. Plus one for rounding.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I asked about in the last pull, but I didn't get any proper answers. Should have cc'd you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@donc This formula just gives you a rough upper bound, managing to introduce four magic numbers instead of one. Why bother with this nonsense when you can just allocate a huge chunk of stack space? Are we planning to compile dmd for an 8-bit micro with 256 bytes of stack?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of those numbers are arbitrary. That's the point. Note that real doesn't have fixed length, it's just bigger than a double. It might be a 128-bit quadruple. Is 256 bytes enough?
Probably, but you have to do a calculation like this one to work it out. I'd rather not do that calculation everytime I read the code.

@ghost ghost reopened this Jan 21, 2013
@ghost
Copy link
Author

ghost commented Jan 21, 2013

This as far as I could get it done while trying to decypher the specs on this page. Format specifiers are unbelievably convoluted. If anyone knows how to get make it print only 2 zero digits let me know.

Edit: Actually the only thing that's necessary is to make at least one trailing zero present if the floating-point is equal to an integral. I'm not sure what the format spec for that would be.

@ghost ghost closed this Jan 21, 2013
@ghost ghost reopened this Feb 8, 2013
@ghost ghost closed this Feb 8, 2013
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants