Results 1 to 20 of 20

Thread: Ceiling in VB [RESOLVED]

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Ceiling in VB [RESOLVED]

    I'm just stuck with this, what's the VB function that yields the "ceiling" of a number, i.e. the smallest integer greater of equal than the number?
    For instance:

    2.1 => 3
    3 => 3
    3.7 => 4

    So it's complementary to the int function that produces the largest integer less or equal than the number.

    If it doesn't exist, what would be a way to emulate it?
    Last edited by krtxmrtz; Aug 2nd, 2004 at 03:00 PM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016
    are you thinking of the Round() function ?

    casey.

  3. #3

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by vbasicgirl
    are you thinking of the Round() function ?

    casey.
    I first thought about it but it's not the same thing, for example,
    Round(3.7 , 0) gives 3 instead of 4.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  4. #4
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951
    You could try something like

    x = mod(number)
    if x > 0 then
    number = int(number + 1)
    endif

  5. #5
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Try:

    VB Code:
    1. Private Function Ceil(sgNumber As Single) As Long
    2.     Ceil = Int(sgNumber) - (sgNumber - Int(sgNumber) > 0)
    3. End Function
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  6. #6
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872
    just add 0.5 to the number you want to round.


    round(1.2 + 0.5, 0 ) => 1
    round(1.7 + 0.5, 0 ) => 2
    - Use the thread tools to Mark your Thread as Resolved when your question is answered.
    - Please Rate my answers if they where helpful.

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    unless the number is negative.


    FIX(-99.8) returns -99, so if you made the number negative, you'd get the next lower number, right?
    Last edited by dglienna; Aug 2nd, 2004 at 02:11 PM.

  8. #8
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    In that case, maybe this is what you need:
    VB Code:
    1. Private Function Ceil(sgNumber As Single) As Long
    2.     Dim iDir As Integer
    3.    
    4.     iDir = IIf(sgNumber < 0, -1, 1)
    5.     Ceil = Int(sgNumber) - (sgNumber - Fix(sgNumber) > 0) * iDir
    6.    
    7. End Function
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  9. #9

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by robbedaya
    just add 0.5 to the number you want to round.


    round(1.2 + 0.5, 0 ) => 1
    round(1.7 + 0.5, 0 ) => 2
    Thank you, but the function f I need must be such that f(1.2) = 2
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  10. #10

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Thumbs up

    Originally posted by Mc Brain
    In that case, maybe this is what you need:
    VB Code:
    1. Private Function Ceil(sgNumber As Single) As Long
    2.     Dim iDir As Integer
    3.    
    4.     iDir = IIf(sgNumber < 0, -1, 1)
    5.     Ceil = Int(sgNumber) - (sgNumber - Fix(sgNumber) > 0) * iDir
    6.    
    7. End Function
    Gracias Emiliano, this function and the previous one you've posted are actually what I was after.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  11. #11
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Por nada. Anyway, just in case you did not understand something...

    Originally posted by krtxmrtz
    Thank you, but the function f I need must be such that f(1.2) = 2
    robbedaya's code did work. If you do

    VB Code:
    1. Debug.Print Round(1.2 + 0.5, 0 )

    you would get 2 as the answer. The only problem was the negative numbers.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  12. #12
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016
    didnt realise you wanted to always round up.

    what about
    VB Code:
    1. MsgBox -Int(-yournumber)

    casey.

  13. #13

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by Mc Brain
    Por nada. Anyway, just in case you did not understand something...

    robbedaya's code did work. If you do

    VB Code:
    1. Debug.Print Round(1.2 + 0.5, 0 )

    you would get 2 as the answer. The only problem was the negative numbers.
    I tried Round to begin with but discarded it because it doesn't work in a few instances where the number has no decimal part. For example,

    y = Round(4 + 0.5, 0)

    produces 4, but

    y = Round(5 + 0.5, 0)

    gives 6.

    Could it be due to the internal floating representation? In principle the binary representations of both 4.5 and 5.5 should have the same mantissa.
    I'd be curious to know why this unexpected behaviour takes place.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  14. #14

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by vbasicgirl
    didnt realise you wanted to always round up.

    what about
    VB Code:
    1. MsgBox -Int(-yournumber)

    casey.
    This one seems to work nicely! I don't know if there may be some erratic behaviour like in the Round function as I've described in my previous post, but I've tested it with a good deal of examples and have found no flaw.

    Thanks casey, the simpler the better.
    Last edited by krtxmrtz; Aug 3rd, 2004 at 03:13 AM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  15. #15
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016
    i think its because round uses bankers rounding.

    eg:
    Round(1.5) = 2
    Round(2.5) = 2
    Round(3.5) = 4
    Round(4.5) = 4


    casey.

  16. #16
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by krtxmrtz
    This one seems to work nicely! I don't know if there may be some erratic behaviour like in the Round function as I've described in my previous post, but I've tested it with a good deal of examples and have found no flaw.

    Thanks casey, the simpler the better.
    Beware with negative numbers. If yournumber is negative you'll get the smaller and not the bigger.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  17. #17
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016
    getting the smaller would be correct wouldnt it because it is actually being greater because of the negative ??

    casey.

  18. #18
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by vbasicgirl
    getting the smaller would be correct wouldnt it because it is actually being greater because of the negative ??

    casey.
    It is... but I understood he needed that way.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  19. #19

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by Mc Brain
    It is... but I understood he needed that way.
    As a matter of fact the figures I'm working with are the results of counting the number of elements in certain groups, so they're always positive. But it's nice to have McBrain's recipe for negative numbers too.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  20. #20

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by vbasicgirl
    i think its because round uses bankers rounding.

    eg:
    Round(1.5) = 2
    Round(2.5) = 2
    Round(3.5) = 4
    Round(4.5) = 4


    casey.
    Are bankers off their onions???
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width