Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

ceil() misbehaving

Chris_D_Mentes_001Chris_D_Mentes_001 Member, csevp Posts: 103 PRO
I'm trying to round up a value but:
1. ceil(1) or ceil(1,1) return 1 and roundToPrecision(2,0) returns 2 as expected.
2. ceil((2405mm-1200mm)/(1200mm+5mm),1) returns 2 ??
3. roundToPrecision((2405mm+5mm)/(1200mm+5mm),0) returns 3 ??
Please tell me what I am doing wrong here!

Best Answers

  • _anton_anton Member, Onshape Employees Posts: 413
    Answer ✓
    #2 is because ((2405mm-1200mm)/(1200mm+5mm) is equal to slightly more than 1 because of floating-point math, and the ceil(..., 1) rounds it up to the next multiple of 1, which is 2.

    For #3, I'm getting 2.
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,714
    Answer ✓
    Yes, everything is stored in meters.
    Senior Director, Technical Services, EMEAI

Answers

  • _anton_anton Member, Onshape Employees Posts: 413
    Answer ✓
    #2 is because ((2405mm-1200mm)/(1200mm+5mm) is equal to slightly more than 1 because of floating-point math, and the ceil(..., 1) rounds it up to the next multiple of 1, which is 2.

    For #3, I'm getting 2.
  • Chris_D_Mentes_001Chris_D_Mentes_001 Member, csevp Posts: 103 PRO
    Okay I think what really gets me is that adding 5mm to 1200mm is where the error begins. This seems like it shouldn't happen even with rounding error. From what I've seen onshape uses IEEE standard 64 bit floating arithmatic.

    According to this link https://www.h-schmidt.net/FloatConverter/IEEE754.html I can see that I shouldn't have errors unless rather then FS units storing values with units as milimeters it actually stores them as meters. In other words 1200mm in the value with units type map is stored as 1.200 and units is meter.

    This seems to be consistant as I've tested it with a couple FS scripts:
    annotation { "Feature Type Name" : "SUM Print" }
    export const PrintMe = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "X" }
            isLength(definition.X, LENGTH_BOUNDS);
            
            annotation { "Name" : "Y" }
            isLength(definition.Y, LENGTH_BOUNDS);
        }
        {
            var X = definition.X;
            var Y = definition.Y;
            println(X+Y);
        });
    
    annotation { "Feature Type Name" : "ADDfix Print" }
    export const PrintMeADDfix = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "X" }
            isLength(definition.X, LENGTH_BOUNDS);
            
            annotation { "Name" : "Y" }
            isLength(definition.Y, LENGTH_BOUNDS);
        }
        {
            var X = definition.X*1000;
            var Y = definition.Y*1000;
            println((X+Y)/1000);
        });
    
    /*
    results in:
    1.2049999999999998 meter
    1.205 meter
    when I enter 1200mm and 5mm as X and Y
    */
    
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,714
    Answer ✓
    Yes, everything is stored in meters.
    Senior Director, Technical Services, EMEAI
  • Chris_D_Mentes_001Chris_D_Mentes_001 Member, csevp Posts: 103 PRO
    You're all beautiful people thank you!
  • Chris_D_Mentes_001Chris_D_Mentes_001 Member, csevp Posts: 103 PRO

    For those who are in rounding hell as I have been for the last 3 hours heres what you need to know,
    do not work with valuewithunits. Multiply you value by 1000 and divide my millimeter. after this you can manipulate this number to your hears contet only going back to value with units at the last second

  • Chris_D_Mentes_001Chris_D_Mentes_001 Member, csevp Posts: 103 PRO

    To elaborate further values in onshape are stored using the IEEE standard 64 bit floating point number, however the "decimal" representation makes a meter 1 not 1000. Because of this the actual number stored has an error which you can see using this link: https://www.h-schmidt.net/FloatConverter/IEEE754.html.

    As an example 50 millimeters is actually 50.0000007450580596923828125 millimeters:

    BUT if you multiply by 1000 all of a sudden you elimiate the floating point error (or at least shift it further back):

    hence the recomended practice

Sign In or Register to comment.