Sunday, April 4, 2010

Perl and The Time It Owes Me

These days I'm working on a my final Verilog project, where I create a simulator for a basic Verilog syntax (netlist only) in Verilog.

The first stage in creating such simulator is parsing the input .v file into two data structures that contain the netlist and the list of cells in the file. This code was given to us by the instructor, and it's written in Perl.

To gather more knowledge about our design, I've decided to create a Python Verilog simulator, similar in concepts to the one we're going to create and verify that it's behaving like the well-known software simulator that we're working with.

After fixing some small bugs, I've noticed that my code generates some events in one time-unit less than the real simulator. After checking my code again and verifying that no bugs lie there I checked the tables that were created by the Perl script that my instructor wrote, and there it was: one line of a bad parsed data.

The parser takes a line that looks like "bla bla bla 1.974 bla bla bla", parses it using a Regular Expression and converts the number to a floating point number (which is always in the format d.ddd) to integer by multiplying it by 1000 and then formatting it using sprintf() (casting to int using the int() function yields the same results).

Looks fine, ain't it? That's what I thought too, but it seems that there's a floating point error that causes a lot of trouble for specific numbers, like 1.007. Then after conversion you get 1006 and some temporary madness.

If you don't believe me, check the following code:

$num = 1.007;
print "Floating number: $num\n";

$newNum = $num * 1000;
print "Possibly integer: $newNum\n";

print sprintf("Real integer: %d\n", $newNum);

Say it's a bug, say it's a feature, I don't care.

This was tested under Perl 5.10.0 under Linux and Windows.

Update: to clear this out, the problem lies in rounding of floating point numbers, and probably exists in every implementation that uses the IEEE representation format. The solution as I see it right now is to use string formatting "%.0f" to convert the floating point variable to string with rounding and not truncation (that's what casting to integer does) and then convert it from string to integer.

Thanks goes to lorg and Michael for clarifying the real facts.

Update 2: it seems that this "feature" doesn't apply to C, because these numbers can be represented in the regular IEEE standards. Now it's a bit weird to me that Perl and Python act exactly the same (any suggestions?).

81 comments:

lorg said...

Well, that's a property of floating point numbers and how they are represented. For example, in Python:

In [1]: x = 1.007

In [2]: print x
1.007

In [3]: y = x*1000

In [4]: print int(y)
1006

The problem seems to me that it uses floating point numbers for parsing something that doesn't behave like a floating point number. A decimal would probably be better, or just using an integer.

StatusReport said...

So I should change the title of the post to "IEEE and The Time It Owes Me" instead. Still, I would expect at least some kind of conversion warning here (it's easy to see it when looking at the mantissa, I'm pretty sure), don't you think the same?

I solved it by a very ugly hack - treated the number as a string, removed the dot and then converted the string to integer. Ugly but it works (and I don't need something good looking here).

Unknown said...

At which point did you expect a conversion warning? The loss of precision happens already at the
"x = 1.007" point. I don't think a warning of "x can not precisely represent the assigned value" would be very beneficial there, since that would be true for almost any assignment of that sort.

So I'm sorry, no sympathy here on this one. It's your responsibility to use data types as God (or IEEE) intended.

StatusReport said...

If not issue a conversion warning, at least do not mask the floating point errors caused. Perl does hide it by displaying 1007 after multiplication. Python, however, displays 1006.9999... which is the appropriate floating point value.

Unknown said...

It's not masking an error, as such. You need to ask yourself what a user (that is, programmer) usually wants when she prints out a floating-point value.

The modern convention is that the user wants a rounded value. So if the precise value is something like 1006.999999994267, then as long as you're rounding to less than some number of digits after the decimal point, you'll get 1007.00... which will be printed as 1007. Perl, by default, rounds to 6 digits. I believe that's the right thing - the number printed really ought to be 1007. If you want something else, like the precise value, or rounding to some other number of digits - specify it explicitly.

The problems start when instead of using a function that rounds, you use one that truncates. For instance, that's what C's numeric type casts do. And that convention is naturally followed by int(). So, if you want to blame someone, blame K&R.

Regarding sprintf... well, the perl sprintf also explicitly tries to emulate the C behaviour. But in C, if you try to print out a floating point value with %d, you'll get nonsense. So the closest emulation they thought of was probably C-style casting (with int()) and then C-style printing. I haven't checked, but I have the feeling that if instead of %d you use the proper format specifier ("%.0f") you'll get the right thing.

StatusReport said...

I agree with what you said, and yes, "%.0f" does solve the problem.

Actually what I truly needed is to use "%x", which probably converts to integer and then to hex representation, so the workaround here is to use "%.0f" and then convert it to whatever you'd like.

erectile dysfunction medications said...

I needed to thank you for this very good read!! I absolutely loved every bit of it. I've got you book marked to look at new stuff you post…

ed meds said...

I don't even know how I finished up right here, but I believed this post was once great. I don't recognise who you are however definitely you are going to a well-known blogger if you are not already. Cheers!

best erectile dysfunction pills said...

Pretty! This was an extremely wonderful article. Many thanks for providing these details.

erection pills online viagra said...

You really make it seem so easy with your presentation but I find this matter to be really something that I think I would never understand. It seems too complicated and very broad for me. I'm looking forward for your next post, I will try to get the hang of it!

best ed pills said...

Please let me know if you're looking for a author for your blog. You have some really good posts and I believe I would be a good asset. If you ever want to take some of the load off, I'd really like to write some articles for your blog in exchange for a link back to mine. Please shoot me an email if interested. Thanks!

erectile dysfunction pills said...

I'm truly enjoying the design and layout of your website. It's a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a designer to create your theme? Exceptional work!

erection pills online viagra said...

Hi there to every , as I am actually keen of reading this blog's post to be updated regularly. It carries fastidious stuff.

top erection pills said...

I enjoy what you guys are usually up too. This type of clever work and exposure! Keep up the excellent works guys I've incorporated you guys to our blogroll.

ed meds said...

It is the best time to make some plans for the longer term and it is time to be happy. I have learn this post and if I may I want to counsel you some attention-grabbing things or suggestions. Perhaps you could write next articles regarding this article. I desire to learn even more issues approximately it!

erectile dysfunction drugs said...

Appreciate this post. Let me try it out.

erectile said...

I'm gone to tell my little brother, that he should also pay a quick visit this blog on regular basis to obtain updated from latest news.

erection pills viagra online said...

I've been exploring for a bit for any high quality articles or blog posts in this sort of area . Exploring in Yahoo I finally stumbled upon this web site. Reading this info So i'm glad to convey that I have an incredibly excellent uncanny feeling I came upon just what I needed. I most for sure will make sure to do not disregard this website and give it a glance on a relentless basis.

erectile dysfunction pills said...

Hey just wanted to give you a quick heads up. The text in your post seem to be running off the screen in Opera. I'm not sure if this is a format issue or something to do with internet browser compatibility but I figured I'd post to let you know. The layout look great though! Hope you get the issue fixed soon. Kudos

impotence said...

Awesome! Its actually amazing post, I have got much clear idea on the topic of from this piece of writing.

erectile dysfunction medications said...

Hello! I've been reading your web site for a while now and finally got the courage to go ahead and give you a shout out from Houston Tx! Just wanted to say keep up the good job!

cheap erectile dysfunction pills online said...

I do believe all the ideas you have introduced on your post. They're very convincing and will definitely work. Nonetheless, the posts are too short for novices. Could you please lengthen them a little from subsequent time? Thank you for the post.

impotence said...

Pretty! This was a really wonderful post. Thanks for providing this info.

top erection pills said...

I was wondering if you ever considered changing the layout of your website? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having 1 or two images. Maybe you could space it out better?

ed medication said...

Thanks in support of sharing such a good thought, post is good, thats why i have read it entirely

herbal erection pills said...

This design is spectacular! You certainly know how to keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well, almost...HaHa!) Wonderful job. I really enjoyed what you had to say, and more than that, how you presented it. Too cool!

erectile dysfunction pills said...

Thanks for some other informative web site. The place else could I am getting that type of info written in such a perfect manner? I've a mission that I'm simply now operating on, and I have been on the glance out for such info.

erectile dysfunction medications said...

Hey there, You have done a great job. I will certainly digg it and personally recommend to my friends. I'm sure they will be benefited from this web site.

best ed pills said...

Saved as a favorite, I really like your website!

buy erection pills said...

Howdy! This is kind of off topic but I need some guidance from an established blog. Is it hard to set up your own blog? I'm not very techincal but I can figure things out pretty fast. I'm thinking about setting up my own but I'm not sure where to start. Do you have any ideas or suggestions? Thank you

erectile dysfunction treatment said...

I'm really enjoying the design and layout of your blog. It's a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a designer to create your theme? Fantastic work!

best erectile dysfunction pills said...

We're a group of volunteers and opening a new scheme in our community. Your site provided us with valuable information to work on. You've performed a formidable job and our entire neighborhood might be thankful to you.

best erectile dysfunction pills said...

With havin so much written content do you ever run into any issues of plagorism or copyright violation? My website has a lot of completely unique content I've either written myself or outsourced but it appears a lot of it is popping it up all over the web without my agreement. Do you know any solutions to help stop content from being stolen? I'd certainly appreciate it.

erection pills said...

I am really loving the theme/design of your blog. Do you ever run into any browser compatibility problems? A number of my blog visitors have complained about my website not operating correctly in Explorer but looks great in Opera. Do you have any recommendations to help fix this issue?

online erectile dysfunction prescription said...

This piece of writing is really a pleasant one it helps new internet visitors, who are wishing for blogging.

Anonymous said...

Excellent blog you have here but I was wondering if you knew of any community forums that cover the same topics talked about here? I'd really like to be a part of group where I can get feedback from other knowledgeable people that share the same interest. If you have any suggestions, please let me know. Kudos!

erection pills viagra online said...

As the admin of this website is working, no doubt very soon it will be well-known, due to its quality contents.

online erectile dysfunction prescription said...

Thank you a bunch for sharing this with all of us you really recognise what you're speaking approximately! Bookmarked. Kindly additionally consult with my website =). We will have a hyperlink change arrangement between us

cheap erectile dysfunction pills online said...

Greate pieces. Keep posting such kind of information on your page. Im really impressed by it.
Hello there, You've performed an incredible job. I will certainly digg it and individually recommend to my friends. I'm confident they will be benefited from this site.

mens ed pills said...

Hello, just wanted to mention, I loved this article. It was funny. Keep on posting!

erectile dysfunction drugs said...

Do you have a spam problem on this website; I also am a blogger, and I was wanting to know your situation; we have developed some nice methods and we are looking to swap strategies with others, why not shoot me an e-mail if interested.

ed treatment tablets said...

It's remarkable designed for me to have a site, which is good designed for my knowledge. thanks admin

erectile dysfunction remedies said...

I'm gone to convey my little brother, that he should also go to see this weblog on regular basis to get updated from hottest information.

erection pills viagra online said...

Hi there, just wanted to tell you, I liked this post. It was inspiring. Keep on posting!

erectile dysfunction treatment said...

Heya i am for the first time here. I found this board and I to find It really useful & it helped me out much. I'm hoping to offer something back and aid others like you helped me.

ed pills online said...

Thanks for sharing your thoughts on %meta_keyword%. Regards

cheap erectile dysfunction pills online said...

Fantastic beat ! I wish to apprentice whilst you amend your web site, how can i subscribe for a weblog web site? The account aided me a appropriate deal. I have been a little bit familiar of this your broadcast offered shiny clear concept

erectile dysfunction causes said...

Excellent goods from you, man. I have understand your stuff previous to and you're just too fantastic. I really like what you have acquired here, really like what you're stating and the way in which you say it. You make it entertaining and you still care for to keep it smart. I cant wait to read much more from you. This is actually a terrific site.

impotence said...

Wonderful website. Lots of helpful info here. I am sending it to a few buddies ans also sharing in delicious. And naturally, thank you to your effort!

top erection pills said...

Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Appreciate it

cheap erectile dysfunction pills online said...

Very nice blog post. I absolutely appreciate this site. Keep writing!

erection pills viagra online said...

Thank you for the auspicious writeup. It in fact used to be a amusement account it. Look complicated to more delivered agreeable from you! By the way, how can we keep up a correspondence?

erectile dysfunction drug said...

This is very interesting, You are a very skilled blogger. I've joined your rss feed and look forward to seeking more of your great post. Also, I've shared your web site in my social networks!

erectile dysfunction remedies said...

Hi! Do you use Twitter? I'd like to follow you if that would be okay. I'm definitely enjoying your blog and look forward to new posts.

ed pills said...

May I simply just say what a relief to discover a person that truly understands what they are discussing on the net. You actually understand how to bring an issue to light and make it important. A lot more people really need to check this out and understand this side of your story. I was surprised you are not more popular given that you definitely possess the gift.

best ed pills said...

Why visitors still make use of to read news papers when in this technological globe everything is available on web?

erectile dysfunction pills said...

I'm really loving the theme/design of your site. Do you ever run into any browser compatibility problems? A few of my blog readers have complained about my blog not operating correctly in Explorer but looks great in Firefox. Do you have any ideas to help fix this issue?

herbal erection pills said...

It's awesome in support of me to have a web page, which is helpful designed for my know-how. thanks admin

best erectile dysfunction pills said...

Hey there! I simply would like to offer you a big thumbs up for your excellent information you have here on this post. I will be coming back to your site for more soon.

impotence said...

Saved as a favorite, I love your blog!

erectile dysfunction causes said...

Do you have a spam issue on this website; I also am a blogger, and I was wondering your situation; we have developed some nice practices and we are looking to swap methods with others, please shoot me an email if interested.

ed medication said...

I'm impressed, I must say. Rarely do I encounter a blog that's both equally educative and amusing, and without a doubt, you have hit the nail on the head. The problem is something that too few folks are speaking intelligently about. I'm very happy I found this during my hunt for something regarding this.

ed pills said...

Hey! I could have sworn I've been to this site before but after browsing through some of the post I realized it's new to me. Nonetheless, I'm definitely glad I found it and I'll be book-marking and checking back frequently!

herbal erection pills said...

hi!,I like your writing very so much! share we keep in touch more approximately your post on AOL? I need a specialist on this area to resolve my problem. May be that's you! Having a look ahead to peer you.

erection pills said...

I'm not sure why but this site is loading incredibly slow for me. Is anyone else having this problem or is it a problem on my end? I'll check back later on and see if the problem still exists.

erectile dysfunction said...

I absolutely love your website.. Very nice colors & theme. Did you make this web site yourself? Please reply back as I'm planning to create my own website and would like to find out where you got this from or what the theme is called. Appreciate it!

erectile dysfunction drug said...

Heya are using Wordpress for your site platform? I'm new to the blog world but I'm trying to get started and create my own. Do you require any coding knowledge to make your own blog? Any help would be greatly appreciated!

ed medication said...

Heya i'm for the first time here. I came across this board and I find It truly useful & it helped me out a lot. I hope to give something back and aid others like you helped me.

ed medication said...

This is very attention-grabbing, You are a very skilled blogger. I have joined your feed and sit up for in quest of more of your great post. Additionally, I have shared your website in my social networks

erectile dysfunction said...

Hey There. I found your blog using msn. This is an extremely well written article. I'll be sure to bookmark it and return to read more of your useful information. Thanks for the post. I'll certainly comeback.

ed pills online said...

I do not know whether it's just me or if everyone else encountering problems with your website. It appears like some of the written text within your posts are running off the screen. Can somebody else please comment and let me know if this is happening to them as well? This may be a problem with my browser because I've had this happen before. Thanks

erectile dysfunction drugs said...

When I initially commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get several e-mails with the same comment. Is there any way you can remove me from that service? Thanks a lot!

erection pills viagra online said...

Nice post. I learn something totally new and challenging on websites I stumbleupon on a daily basis. It will always be interesting to read content from other authors and use something from their websites.

ed pills online said...

Greate post. Keep writing such kind of information on your page. Im really impressed by your site.
Hi there, You've done a fantastic job. I will definitely digg it and personally recommend to my friends. I'm sure they'll be benefited from this web site.

impotence said...

Howdy! I could have sworn I've been to this site before but after browsing through some of the post I realized it's new to me. Anyways, I'm definitely glad I found it and I'll be book-marking and checking back often!

mens ed pills said...

Hi! I've been following your weblog for some time now and finally got the bravery to go ahead and give you a shout out from Dallas Texas! Just wanted to say keep up the good work!

erectile dysfunction drug said...

Great beat ! I would like to apprentice while you amend your website, how could i subscribe for a blog site? The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast offered bright clear idea

erection pills said...

Howdy! This blog post could not be written much better! Looking through this article reminds me of my previous roommate! He always kept preaching about this. I am going to forward this information to him. Fairly certain he'll have a great read. Thank you for sharing!

best ed pills said...

Hi there! I know this is somewhat off topic but I was wondering which blog platform are you using for this site? I'm getting tired of Wordpress because I've had problems with hackers and I'm looking at options for another platform. I would be awesome if you could point me in the direction of a good platform.

erectile dysfunction remedies said...

I needed to thank you for this very good read!! I certainly loved every little bit of it. I have got you book marked to check out new things you post…

dysfunction said...

Hi every one, here every one is sharing such experience, so it's pleasant to read this weblog, and I used to go to see this webpage every day.