Solved

Faster than FloatToStr command...?

Posted on 2014-11-10
12
92 Views
Last Modified: 2015-05-30
Hello all, does anyone know of a faster/more efficient alternative to the Delphi FloatToStr function?

Thanks
   Shawn

P.S: I'm using Delphi 7.
0
Comment
Question by:shawn857
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 27

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 500 total points
ID: 40434440
I use win format function:
Format('%5.2f', [123.456789]);

Open in new window


hint: if you interested to see other faster algorithms:
http://fastcode.sourceforge.net/
0
 

Author Comment

by:shawn857
ID: 40434443
thanks Sinisa. Regarding the Format function - I won't know what size the real number will be beforehand. It could be 12.8 or 12228763521.65255362. So would I be able to use a Format statement like Format('%30.20f') ?

I looked briefly at the fastcode page. Do you know if there's a "FloatToStr" function there?

Thanks!
    Shawn
0
 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 40434457
No, there is not FloatToStr (yet) on fastcode page.
I don't suggest to show more than 8 decimal places - and this should be fine too:

Label1.Caption := Format('%.8f', [12228763521.65255362]);

Open in new window


... or

Label1.Caption := Format('%f', [12228763521.65255362]);

Open in new window


... with default formatting.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:shawn857
ID: 40434463
Thanks Sinisa, I'll try that.

Looking at the Fastcode page, I noticed there were a few other routines that would be useful for me. I'm a little confused about the usage though. It seems for each fucntion, there are many different ways to call it:

function  XXXFastcodeP4N;
function  XXXFastcodeP4P;
function  XXXFastcodePMD;
function  XXXFastcodePMB;
function  XXXFastcodeAthlonXP;
function  XXXFastcodeAMD64;

I don't really want a "hardware-specific" version...Is any one of these okay, or do you know if there is just a regular "generic" way to call the function?

Thanks!
    Shawn
0
 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 40434482
Using assembler variant of function for Intel processors is a good way too.
0
 

Author Comment

by:shawn857
ID: 40436330
Thanks Sinisa. How do I know which one is the assembler variant function for intel processors?

Thanks
   Shawn
0
 

Accepted Solution

by:
shawn857 earned 0 total points
ID: 40436368
Sinisa, I just tried a small profiling run comparing FloatToStr and your suggestion of "Format" command... FloatToStr is actually slightly faster:

FloatToStr -   0.00000074 secs Avg Time
Format('%f' - 0.00000084 secs Avg Time

... maybe there is just no improving on FloatToStr?

Cheers
    Shawn
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 40438853
look at the complete picture ... for improvements
does the input have to be a float ?

this may be an odd question ... but what if you got an integer for the integer part
and an integer for the fractional part

inttostr is probably somewhat faster than floattostr
0
 

Author Comment

by:shawn857
ID: 40460947
Hi Geert, well, that won't help for the odd times when the float number happens to be in Scientific Notation.

Thanks
   Shawn
0
 

Expert Comment

by:shawn226
ID: 40795163
Yes, we can just abandon it... sorry for that. A pretty nit-picky question from me to begin with actually...

Regards
   Shawn
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question