AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Get number digits

Ein Thema von WojTec · begonnen am 12. Dez 2013 · letzter Beitrag vom 12. Dez 2013
 
Benutzerbild von Aphton
Aphton

Registriert seit: 31. Mai 2009
1.198 Beiträge
 
Turbo Delphi für Win32
 
#7

AW: Get number digits

  Alt 12. Dez 2013, 18:41
Here are the mathematical fucntions you need:

digitCount(x) := ⌈log10(x+1)⌉
extractDigit(x, digitIdx) := ⌊x / 10^(digitCount(x)-digitIdx)⌋ mod 10

(sidenote - "⌈⌉" stands for ceiling, "⌊⌋" for flooring)

Example number = 1234
digitCount(1234) = ⌈log10(1234+1)⌉
= ⌈3.09⌉
= 4

extractDigit(1234, 2) = ⌊1234 / 10^(4 - 2)⌋ mod 10
= ⌊1234 / 10^2⌋ mod 10
= ⌊1234 / 100⌋ mod 10
= ⌊12.34⌋ mod 10
= 12 mod 10
= 2

Edit: This is mathematically as low as it gets.. Cant think of a shorter way right now.
Bitshifting works on base 2. You would have to remap which is an extra overhead that is not needed.
There are assembler instructions (SSE, SSE2, ..) that are faster.
As far as I know the math.pas functions use the optimized assembler operations. Use it!
das Erkennen beginnt, wenn der Erkennende vom zu Erkennenden Abstand nimmt
MfG

Geändert von Aphton (12. Dez 2013 um 19:09 Uhr)
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 04:25 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz