GithubHelp home page GithubHelp logo

Comments (3)

tavinus avatar tavinus commented on September 28, 2024

I am not sure we should ever get a negative value in pdfScale's use case.

I would still like to fix the function though, but I would probably add the extra code inside de function.

pdfScale already uses BC for many things. It is already a requirement, so it would not be a problem to use a BC solution (if it is better).

How about just changing > to -gt as suggested?

I will need to test this a bit, before fixing it.

from pdfscale.

tavinus avatar tavinus commented on September 28, 2024

Ok, was able to test it properly now.

The old function isFloatBiggerThanZero() was indeed busted. Changing to -gt just makes it worse, since it breaks execution when it finds a point (eg 2.8).

Made this little script to test and be sure

#!/bin/bash

TRUE=0                     # Silly stuff
FALSE=1

# Test script for float functions

# Returns $TRUE if $1 is a floating point number (or an integer), $FALSE otherwise
isFloat() {
        [[ -n "$1" && "$1" =~ ^-?[0-9]*([.][0-9]+)?$ ]] && return $TRUE
        return $FALSE
}

# Returns $TRUE if $1 is a floating point number bigger than zero, $FALSE otherwise
isFloatBiggerThanZero() {
        isFloat "$1" && [[ "$1" =~ ^0*[1-9] || "$1" =~ ^0*[.]0*[1-9] ]] && return $TRUE
        return $FALSE
}

# Returns $TRUE if $1 is a floating point number between 0 and 1, $FALSE otherwise
isFloatPercentage() {
        [[ -n "$1" && "$1" =~ ^-?[0]*([.][0-9]+)?$ ]] && return $TRUE
        [[ "$1" == "1" ]] && return $TRUE
        return $FALSE
}

syes() { echo 'yes => '"$1" ; }
sno()  { echo 'no  => '"$1" ; }

v='text' ; isFloat "$v" && syes "$v" || sno "$v"
v=3      ; isFloat "$v" && syes "$v" || sno "$v"
v='1.2'  ; isFloat "$v" && syes "$v" || sno "$v"
v='0.5'  ; isFloat "$v" && syes "$v" || sno "$v"
v='-0.7' ; isFloat "$v" && syes "$v" || sno "$v"
v='-2.5' ; isFloat "$v" && syes "$v" || sno "$v"

echo '----------------'

v='text' ; isFloatBiggerThanZero "$v" && syes "$v" || sno "$v"
v=3      ; isFloatBiggerThanZero "$v" && syes "$v" || sno "$v"
v='1.2'  ; isFloatBiggerThanZero "$v" && syes "$v" || sno "$v"
v='0.5'  ; isFloatBiggerThanZero "$v" && syes "$v" || sno "$v"
v='-0.7' ; isFloatBiggerThanZero "$v" && syes "$v" || sno "$v"
v='-2.5' ; isFloatBiggerThanZero "$v" && syes "$v" || sno "$v"

echo '----------------'

v='text' ; isFloatPercentage "$v" && syes "$v" || sno "$v"
v=3      ; isFloatPercentage "$v" && syes "$v" || sno "$v"
v='1.2'  ; isFloatPercentage "$v" && syes "$v" || sno "$v"
v='0.5'  ; isFloatPercentage "$v" && syes "$v" || sno "$v"
v='-0.7' ; isFloatPercentage "$v" && syes "$v" || sno "$v"
v='-2.5' ; isFloatPercentage "$v" && syes "$v" || sno "$v"

Outputs

$ ./testFloat.sh 
no  => text
yes => 3
yes => 1.2
yes => 0.5
yes => -0.7
yes => -2.5
----------------
no  => text
yes => 3
yes => 1.2
yes => 0.5
no  => -0.7
no  => -2.5
----------------
no  => text
no  => 3
no  => 1.2
yes => 0.5
yes => -0.7
no  => -2.5

Pls note that my isFloatPercentage only returns $TRUE if it is a value from 0 to 1. That is by design for what the script needs. I don't remember if it had to be positive as well 🤣, but if that is the case I can just also run the isFloatBiggerThanZero on top of it.

I will push the changes now. Thanks for the function prototype!

Cheers!
Gus

from pdfscale.

tavinus avatar tavinus commented on September 28, 2024

Fixed on 1edc9d5
v2.5.8

from pdfscale.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.