in reply to Yet again: floats are not expressed accurately

TIMTOWTDI
DB<13> sub inc_version { my ($ver,$pos) = @_ ; my @vers = split /\./ +,$ver; $vers[$pos]++; return join '.', @vers } DB<14> p inc_version("2.26.3",$_),"\n" for 0..2 3.26.3 2.27.3 2.26.4

tho you might want to consider sprintf to format to 3 digits

edit

ehm ...

> newversion = version + 0.01;

.... so 2.99 becomes 3.00 ? I hope it's clear now why you shouldn't use floats...

Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: Yet again: floats are not expressed accurately
by syphilis (Archbishop) on Mar 21, 2023 at 13:31 UTC
    I hope it's clear now why you shouldn't use floats...

    Yes - version numbers are actually integers separated by dots. To increment, just ++ the final integer.

    Cheers,
    Rob
Re^2: Yet again: floats are not expressed accurately
by bliako (Abbot) on Mar 21, 2023 at 13:04 UTC

    granted! (for versions).I just went to the bank because I needed 226 drachmas but decided to draw 2.26 at a time... as these are thought experiments I am a at a loss as to how much i have in my pocket and how much still in my account...

      Don't be so drachmatic!!! ;P

      Some languages (raku?) are trying to solve the issue by having a number type for fractions°. Hence integer operations behind the scenes.

      And that's what you should do with your West Asian island coins.

      Add them as integers and move the point afterwards.

      Sorry, we had this discussion already so often that I'm not too motivated to further discuss it.˛

      FWIW

      °) there was a pragma to turn scalars into slow objects doing just this.

      use fract or something...

      Updates

      https://perldoc.perl.org/bigrat but it doesn't seem to support dot-notation...

      ˛) See https://floating-point-gui.de/ for more.

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery